سؤال

I filled out the form, and received data on the server like this:

model.Firstname = HttpContext.Current.Request.QueryString["Firstname"];
model.Lastname = HttpContext.Current.Request.QueryString["Lastname"];
model.Middlename = HttpContext.Current.Request.QueryString["Middlename"];
model.Avatar = HttpContext.Current.Request.QueryString["Avatar"];
model.Birthday = HttpContext.Current.Request.QueryString["Birthday"];

Model:

public int CustomerID { get; set; }
public int StatusID { get; set; }
public string Firstname { get; set; }
public string Lastname{ get; set; }
public string Middlename { get; set; }
public string Birthday { get; set; }

Is there any way to make it easier and combine this lines?

هل كانت مفيدة؟

المحلول

You can write an extension method and use reflection to set your properties, like this:

public static void SetProperties<T>(this T source, HttpContext context)
{
     var properties = typeof(T)
             .GetProperties(BindingFlags.Instance | BindingFlags.Public);

     var  values = context.Request.QueryString;

     foreach (var prop in properties)
     {
         if (values.AllKeys.Contains(prop.Name))
         {
             prop.SetValue(source,values[prop.Name]);
         }
     }
}

Usage:

mode.SetProperties(HttpContext.Current);

This assumes that your keys in the query string are exactly matches with your property names of the model.

نصائح أخرى

If you are using asp.net mvc, you don't have to specifically assign each property through the HttpContext, you can directly send the serialized data of your form and receive as strongly typed model object in your function, something like :

[HttpPost]
public TMServiceResponse Customer_Save(CustomerModel obj)
{
     //make your form actionmethod like this
}

and post it from the view like this:

var form = jQuery("#myformId");
jQuery(".btnCustomerSubmit").on("click", function (e) {
    e.preventDefault();
    var result = form.valid();
    if (result === true) {
        var url = '/ControllerName/Customer_Save';
        var data = form.serialize();
        //send post request using jQuery Ajax
        $.ajax({
           url: url,
           type: "post",
           data: data,
           success:function(msg){}
        });
    }
});

where .btnCustomerSubmit represents the submit button of your form, just that, it is usual button, with a click handler and post request is sent explicitly

You can try Automapper. AFAIK it doesn't have Dictionary to Object mapping out-of-the-box, but here i found an example how to do that with few custom code:

http://automapper.codeplex.com/workitem/3266

I would do it like this:

var queryString = HttpContext.Current.Request.QueryString;
var model = new Model 
{
    Firstname = queryString["Firstname"],
    Lastname = queryString["Lastname"],
    Middlename = queryString["Middlename"],
    Avatar = queryString["Avatar"],
    Birthday = queryString["Birthday"]
};

This is faster than reflection and more reliable. It won't start failing (or silently skip items) if someone renames Model.Firstname to Model.FirstName.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top