【发布时间】:2010-12-30 13:53:31
【问题描述】:
我有一个视图,其中有一个局部视图,其中有一个文本框。主视图有一个 person 类型的模型,部分视图有一个 person.other 类型的模型。当我做一个 ajax 回传时,另一个模型是空的,我希望它能够获取文本框数据。这是代码;
类
public class Person
{
public string PersonID { get; set; }
public string Name { get; set; }
public Other Other { get; set; }
}
public class Other
{
public string OtherName { get; set; }
}
控制器
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index()
{
Person person = new Person();
person.Other = new Other();
person.Other.OtherName = "avbc";
return View(person);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Test(Other other)
{
if (Request.IsAjaxRequest())
{
return PartialView("Test");
}
return null;
}
查看
@model PartialViewTest.Models.Person
<h2>Index</h2>
<div id="mydiv">
@Html.Partial("Test", Model.Other)
</div>
局部视图
@model PartialViewTest.Models.Other
<h1>Test</h1>
@using (Html.BeginForm("Test", "Home", FormMethod.Post, new { id = "testForm" })) {
@Html.TextBoxFor(m => m.OtherName)
<input type="submit"/>
}
jquery 提交
$(document).ready(function () {
$('#testForm').submit(function () {
$.post($(this).attr("action"),
$(this).serialize(),
function (result) {
$('#mydiv').html(result);
});
);
});
【问题讨论】: