【发布时间】:2014-11-16 17:04:50
【问题描述】:
我正在创建一个模型并将其传递给局部视图。当我提交模型时 ModelStat.IsValid 为真,但无论我在表单上输入什么值,它的属性都为空。
控制器和模型
public class TestController : Controller
{
// GET: Test
public ActionResult Index()
{
TestModel model = new TestModel();
model.SomeFieldName= "Test";
model.OtherFieldName = "AnotherTest";
return PartialView(model);
}
[HttpPost]
public PartialViewResult Index(TestModel model)
{
if(ModeState.IsValid)
{
//Do Stuff to model
}
return PartialView(model);
}
public class TestModel
{
[Required]
public string SomeFieldName;
[Required]
public string OtherFieldName;
}
}
局部视图
@model Portal.Controllers.TestController.TestModel
@using (Ajax.BeginForm("Index", new AjaxOptions { UpdateTargetId = "Content" }))
{
@Html.ValidationSummary(true)
<div id="Content">
@Html.LabelFor(model => model.SomeFieldName,"FieldName")
@Html.TextBoxFor(model => model.SomeFieldName)
@Html.LabelFor(model => model.OtherFieldName ,"OtherFieldName")
@Html.TextBoxFor(model => model.OtherFieldName )
<input type="submit" value="Save" class="btn btn-default" />
</div>
}
看完this post我换了
public PartialViewResult Index(TestModel model){}
与
public PartialViewResult Index(FormCollection model)
{
var val = model["SomeFieldName"];
var otherVal = model["OtherFieldName"];
}
我能够通过 FormCollection 访问这些值,但我无法将它们放入我的模型中。关于是什么让我的模型无法正确填充的任何想法?
【问题讨论】:
标签: jquery ajax asp.net-mvc-5