- 1、方式1:字段加验证
- @model MvcWeb.Models.UserInfo
- @{
- ViewBag.Title = "Add";
- }
- <h2>Add</h2>
- @using (Html.BeginForm())
- {
- @Html.ValidationSummary(true)
- @Html.HiddenFor(model => model.Id)
- <div class="editor-label">
- @Html.LabelFor(model => model.UserName)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.UserName)
- @Html.ValidationMessageFor(model => model.UserName)
- </div>
- <p>
- <input type="submit" value="Save" />
- </p>
- }
- ==============================================
- 2、方式2
- @{
- ViewBag.Title = "Add";
- }
- <h2>Add</h2>
- @using (Html.BeginForm("Add", "UserInfo", FormMethod.Post)) //方法名称,控制器名称
- {
- <table>
- <tr>
- <td>
- 用户名称:
- </td>
- <td>
- @Html.TextBox("txtUserName")
- </td>
- </tr>
- <tr>
- <td>
- 密码:
- </td>
- <td>
- @Html.TextBox("txtPassword", ViewData["url"])
- </td>
- </tr>
- <tr>
- <td>
- <input id="Submit1" type="submit" value="submit" />
- </td>
- <td>
- <input id="Reset1" type="reset" value="reset" />
- </td>
- </tr>
- </table>
- }