试试这个,
模型
public class RegisterModel
{
public int ID { get; set; }
[Required(ErrorMessage = "Name is required.")]
public string Name { get; set; }
[Required(ErrorMessage = "Phone is required.")]
public string PhoneNo { get; set; }
[Required(ErrorMessage = "Address is required.")]
public string Address { get; set; }
}
看法
@model XXX.XXX.RegisterModel
@using (Html.BeginForm("Create", "Test", FormMethod.Post, new { id = "FrmIndex" }))
{
<div>
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name)
@Html.ValidationMessageFor(m => m.Name)
</div>
<br />
<div>
@Html.LabelFor(m => m.Address)
@Html.TextBoxFor(m => m.Address)
@Html.ValidationMessageFor(m => m.Address)
</div>
<br />
<div>
@Html.LabelFor(m => m.PhoneNo)
@Html.TextBoxFor(m => m.PhoneNo)
@Html.ValidationMessageFor(m => m.PhoneNo)
</div>
<input type="submit" value="Save" style="float: left;" id="btnSave" title="btn" name="ButtonType" />
}
控制器
[HttpGet]
public ActionResult Create(RegisterModel model)
{
return View();
}
[HttpPost]
public ActionResult Create(RegisterModel model)
{
if (model != null && ModelState.IsValid) // check to see if any users are missing required fields. if not...
{
if (model.ID != 0)
{
UpdateRegister(model);
}
else
{
InsertRegister(model);
}
}
else
{
return View();
}
return RedirectToAction("Index");
}