【发布时间】:2015-08-24 03:55:40
【问题描述】:
我是 MVC 的新手,我猜这个问题反映了这一点。
我有 2 个 ActionResults - 注册 - 一个在 HttpGet,另一个在 HttpPost。在HttpGet 上,我创建了一个模型实例,根据查询字符串值设置公司属性并将模型传递给视图。到目前为止,一切顺利。
当HttpPost 发生时,公司属性设置为空。好像什么都没有设置。我究竟做错了什么?
注册 HTTPGet
[AllowAnonymous]
public ActionResult Register(Guid Firm)
{
InnuendoContext DB = new InnuendoContext();
RegisterModel RM = new RegisterModel();
RM.Firm = (from F in DB.Firms
where F.FirmId == Firm
select F).FirstOrDefault();
return View(RM);
}
注册 HTTPPost
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register( RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, propertyValues: new
{
Name = model.Name,
Surname = model.Surname,
Firm_FirmID = model.Firm
});
WebSecurity.Login(model.UserName, model.Password);
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
}
查看
@model Innuendo.Models.RegisterModel
@{
ViewBag.Title = "Register";
}
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>Create a new account.</h2>
</hgroup>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>Registration Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name)
</li>
<li>
@Html.LabelFor(m => m.Surname)
@Html.TextBoxFor(m => m.Surname)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
</li>
<li>
@Html.LabelFor(m => m.ConfirmPassword)
@Html.PasswordFor(m => m.ConfirmPassword)
</li>
</ol>
<input type="submit" value="Register" />
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
型号
namespace Innuendo.Models
{
[Table("Firms")]
public class FirmModel
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public Guid FirmId { get; set; }
[Display(Name = "Firm Name")]
[Required]
[StringLength(250)]
public string Name { get; set; }
[Required]
public virtual AddressModel Address { get; set; }
[StringLength(250)]
public string LogoPath { get; set; }
}
}
【问题讨论】:
标签: asp.net-mvc