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>
- }
----------------------
<%using (Html.BeginForm("Create", "Book")) { %> <div>//这里是你要提交的表单信息 <div> <input type="submit" id="submit" name="submit" value="搜索" /> </div> <%} %>
如果html表单中不使用@Html控件,直接写html控件,那么控制器中,要通过 FormCollection form: form["txtAccount"]; 取值
public ActionResult UserRegister(FormCollection form) { RegisterModel reg = new RegisterModel(); reg.Account = form["txtAccount"]; if (form["txtAccount"]!="huika123") {
} return View(); }