gpt4 book ai didi

asp.net-mvc-3 - 在 mvc 3 中的单个按钮提交的单个 View 上保存来自不同部分 View 的数据

转载 作者:行者123 更新时间:2023-12-04 06:04:17 24 4
gpt4 key购买 nike

我有三个类(class)

  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; }
}

我为三个不同的模型制作了三个局部 View ,并将其渲染到一个页面中,如下所示
@{
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>
}

我想要的是当我单击提交按钮时,来自部分 View 的所有数据都应该到达 [httppost],在那里我可以将数据保存到 User,Education,Login Tables。如何将数据放入具有 http post Controller 的 Controller 中,例如:
[HttpPost]
public ActionResult Register(?,?,?)
{
context.Logins.Add(LoginObject);
context.Educations.Add(EducationObject);
context.Professions.Add(ProfessionObject);
return View();
}

我只想知道如何将上述部分 View 数据放入 httppost Controller 中,以便我可以如上所述保存数据

我在 Mvc 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

和 Controller Action :
[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);
}

关于asp.net-mvc-3 - 在 mvc 3 中的单个按钮提交的单个 View 上保存来自不同部分 View 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8557117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com