Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 使用MVC3从POST请求设置请求对象属性的合适模式?_Asp.net Mvc 3_C# 4.0_Http Post - Fatal编程技术网

Asp.net mvc 3 使用MVC3从POST请求设置请求对象属性的合适模式?

Asp.net mvc 3 使用MVC3从POST请求设置请求对象属性的合适模式?,asp.net-mvc-3,c#-4.0,http-post,Asp.net Mvc 3,C# 4.0,Http Post,对于MVC3应用程序的传入POST请求,我希望验证传入的请求参数。如果存在无效参数,将引发异常 鉴于以下目标: public class ActionRequest { public string ActionRequestPassword { get; set; } public bool EnableNewsfeedAppPool { get; set; } } 对于传入的post请求,我希望通过以下方式初始化具有适当属性的对象: public class Newsfeed

对于MVC3应用程序的传入POST请求,我希望验证传入的请求参数。如果存在无效参数,将引发异常

鉴于以下目标:

public class ActionRequest
{
    public string ActionRequestPassword { get; set; }
    public bool EnableNewsfeedAppPool { get; set; }
}
对于传入的post请求,我希望通过以下方式初始化具有适当属性的对象:

public class NewsfeedAppPoolController : Controller
{
    [ActionName("EnableAppPool"), AcceptVerbs(HttpVerbs.Post)]
    [ValidateInput(false)]
    [NoCache]
    public ActionResult EnableAppPool(FormCollection formCollection)
    {
        Models.ActionRequest actionRequest = ValidatePOSTRequest(formCollection);

        // do things with actionRequest

        return null;
    }

    private Models.ActionRequest ValidatePOSTRequest(FormCollection formCollection)
    {
        try
        {
            Type actionRequestType = typeof(Models.ActionRequest);
            System.Reflection.PropertyInfo propertyInfo = null;
            object systemActivatorObject = Activator.CreateInstance(actionRequestType);

            foreach (var key in formCollection.AllKeys)
            {

                propertyInfo = typeof(Models.ActionRequest).GetProperty(key);
                Type t = propertyInfo.PropertyType; // t will be System.String


                if (t.Name == "Int32")
                {
                    actionRequestType.GetProperty(key).SetValue(systemActivatorObject, Convert.ToInt32(formCollection[key]), null);
                }
                else
                {
                    actionRequestType.GetProperty(key).SetValue(systemActivatorObject, formCollection[key], null);
                }
            }

            return (Models.ActionRequest)systemActivatorObject;
        }
        catch (Exception ex)
        {
            throw ex;
        } 
    }
}
我想知道这方面是否有任何改进,或者如何以有效的方式实现这一点的建议


谢谢。

ASP.Net MVC已经为您完成了所有这些。
只需将
Models.ActionRequest ActionRequest
参数添加到操作中


如果要添加其他验证逻辑,请使用
System.ComponentModel.DataAnnotations

只需使用默认的模型绑定器,该绑定器将负责从请求参数实例化和绑定ActionRequest:

public class NewsfeedAppPoolController : Controller
{
    [ActionName("EnableAppPool"), AcceptVerbs(HttpVerbs.Post)]
    [ValidateInput(false)]
    [NoCache]
    public ActionResult EnableAppPool(ActionRequest actionRequest)
    {
        // do things with actionRequest

        return null;
    }
}

合适的模式是

[HttpPost]
public ActionResult Save(Employee employee)
{
  if(ModelState.IsValid)
  {
     db.Save(employee);
     RedirectToAction("Index");
  }

  return View();
}
注:

employee
实例由默认模型绑定器根据请求中的可用值(表单、querystring、routedata等)自动创建和填充

默认模型绑定器将值绑定到模型时,它还会进行验证并将所有错误存储在
ModelState
字典中,因此通过检查
ModelState.IsValid
可以知道验证是否成功

要了解有关模型绑定的更多信息,请参阅。
要了解有关模型验证的更多信息,请参阅。

您不需要
[ActionName]