将数据保存在MVC提交3
我有三个类将数据保存在MVC提交3
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对不起,如果我没有任何意义,当我问。请引导我一起
而不是部分我会推荐你使用编辑器模板。这里有一个例子,你如何可以写一个形式,将保存用户对象:
@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);
}
这是一个很好的解决方案。但可以想要问一个小事.1st是用户(你创建的)是一个ViewModel?第二,我们如何在飞行中为登录,教育,用户的编辑器模板声明运行时脚手架?是否有任何出路,以便我们可以在飞行中打开和关闭模型属性的脚手架选项,同时查看不同页面中的编辑选项? – Joy 2011-12-19 08:22:02
@Joy,哦,对不起,我从来没有使用脚手架,也不知道它是如何工作的。我更喜欢定义自己的视图模型和视图。 – 2011-12-19 08:23:51
NP,我会检查出来然后恢复。虽然我猜viewmodel可能暂时解决我的问题。但我真的在寻找脚手架是否能解决我的问题。而不是创造我自己的观点。可能在那里应该存在一个选项,如果我们可以做到这一点,所以我们可以使用ViewModel + EditorTemplates = On Flight Reusable Templates for custom Models :) – Joy 2011-12-19 10:43:42
您在每个部分视图中使用哪些模型? – Eranga 2011-12-19 04:37:52
@Eranga我用他们每个人的登录,职业,教育模型......用于局部视图 – Joy 2011-12-19 06:55:14