我有三个班级
public partial class User
{
public string No_ { get; set; }
public string LastName { get; set; }
public virtual ICollection<Login> Logins { get; set; }
public virtual ICollection<Education> Educations { get; set; }
}
public partial class Education
{
public string No_ { get; set; }
public string UserId { get; set; }
public string Degree { get; set; }
public string Institution { get; set; }
public string Percentage { get; set; }
}
public partial class Login
{
public string No_ { get; set; }
public string UserId { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public virtual User User { get; set; }
}
我为三个不同的模型制作了三个局部视图,并将其渲染成如下页面
@{
ViewBag.Title = "Register";
Layout = "~/Views/Shared/BlankLayout.cshtml";
}
<h2>
Register</h2>@using (@Html.BeginForm())
{
@Html.Partial("LoginPartialView")
@Html.Partial("UserPartialView")
@Html.Partial("ProfessionPartialView")
<section>
<div>
<button class="reset">Reset</button>
<button class="submit" name="submit" value="Submit">Submit</button>
</div>
</section>
}
我想要的是当我点击提交按钮时,部分视图中的所有数据都应该到[httppost],我可以将数据保存到用户,教育,登录表。如何将数据输入到具有http post控制器的控制器中:
[HttpPost]
public ActionResult Register(?,?,?)
{
context.Logins.Add(LoginObject);
context.Educations.Add(EducationObject);
context.Professions.Add(ProfessionObject);
return View();
}
我只是想知道如何将上述部分视图数据放入httppost控制器,以便我可以保存上面提到的数据
我非常喜欢Mvc 3请原谅我,如果我在问我没有意义的话。请指导我
答案 0 :(得分:3)
我建议你使用编辑器模板而不是局部。下面是一个如何编写将保存User对象的表单的示例:
@model User
@{
ViewBag.Title = "Register";
Layout = "~/Views/Shared/BlankLayout.cshtml";
}
<h2>
Register</h2>
@using (@Html.BeginForm())
{
@Html.EditorFor(x => x.No_)
@Html.EditorFor(x => x.LastName)
@Html.EditorFor(x => x.Logins)
@Html.EditorFor(x => x.Educations)
<section>
<div>
<button class="reset">Reset</button>
<button class="submit" name="submit" value="Submit">Submit</button>
</div>
</section>
}
然后是2个相应的编辑器模板:
~/Views/Shared/EditorTemplates/Login.cshtml
:
@model Login
... some input fields for the login
~/Views/Shared/EditorTemplates/Education.cshtml
:
@model Education
... some input fields for the education
和控制器操作:
[HttpPost]
public ActionResult Register(User model)
{
// the model object will be correctly populated from the default model binder
// here we can save it
return View(model);
}