Asp.net mvc MVC将表单发送到页面进行测试
我正在使用MVC,并且有一个包含表单的视图(Asp.net mvc MVC将表单发送到页面进行测试,asp.net-mvc,asp.net-mvc-3,actionresult,Asp.net Mvc,Asp.net Mvc 3,Actionresult,我正在使用MVC,并且有一个包含表单的视图(test.cshtml)。是否有方法将其发送到另一个视图page.cshtml进行测试,而不是相同的[http]控制器ActionResult test() 我试图在更新数据库之前验证所有表单字段值是否正确。有没有更简单的方法 在你看来 @using (Html.BeginForm("Add", "Weight", FormMethod.Post)) //where "Add" is the Action name and Weight is
test.cshtml
)。是否有方法将其发送到另一个视图page.cshtml
进行测试,而不是相同的[http]控制器ActionResult test()
我试图在更新数据库之前验证所有表单字段值是否正确。有没有更简单的方法 在你看来
@using (Html.BeginForm("Add", "Weight", FormMethod.Post))
//where "Add" is the Action name and Weight is the controller (WeightController) -> http://foo/Weight
{
......
}
用模型
public class WeightModel
{
[Required] public string Description { get; set; }
[Required] public int Weight { get; set; }
public string Notes { get; set; }
}
在控制器中
[HttpPost]
public ActionResult Add(WeightModel model)
{
if (!ModelState.IsValid) //framwork will validate based on attributes on model
{
return View("Index", model);
}
else
{
//save to db
return RedirectToAction("Added");
}
}
您可以将其发送到同一控制器中具有其他名称的另一个
ActionResult
。有一种更简单的方法,只需将所有验证放在模型中,然后在保存之前调用ModelState.IsValid
。通过在Html.BeginForm
调用中将它们指定为参数,您可以向任何控制器/操作发送表单