前台使用数据库model和Html.BeginForm("action", "controller", FormMethod.Post)提交表单,在保存到数据库前对表单项进行判断的方法(一)

前端:

@model DBEF.XXX

 @using (Html.BeginForm("action", "controller", FormMethod.Post))
    {
        @Html.ValidationSummary(true)
        <fieldset style="width: 700px; margin: 0 auto;">
            <legend>
                添加
            </legend>

            <div>
                <strong>账号</strong>
            </div>
            <div>
                @Html.EditorFor(model => model.Account, new { @id = "Account" })
                <span style="color:red; font-size: small">
                    @Html.ValidationMessageFor(model => model.Account)
                </span>
            </div>         
            <div style="display:none">
                @Html.HiddenFor(model => model.Pwd, new { @Value = "" })
            </div>
            <div>
                <strong>姓名</strong>
            </div>
            <div>
                @Html.EditorFor(model => model.Name, new { @id = "Name" })
                <span style="color:red; font-size: small">
                    @Html.ValidationMessageFor(model => model.Name)
                </span>
            </div>     
            <div>
                <strong>备注</strong>
            </div>
            <div>
                @Html.EditorFor(model => model.Note, new { @id = "Note" })
            </div>

            <input type="submit" value="添加" id="create" style="margin-right: 20px;" class='bookButton btn btn-success btn-small' />

            @Html.ActionLink("取消", "Index", new { }, new { @class = "btn btn-small btn-danger" })
        </fieldset>

后端:

 public ActionResult action(XXX t)
        {
            string id = Session["userid"].ToString();
            if (string.IsNullOrWhiteSpace(id))
            {
                return RedirectToAction("Login", "Home");
            }
            else
            {
                try
                {
                    var accountFind = db.XXX.Where(m => m.Account == t.Account);
                    if (accountFind.Count() > 0)
                    {                       
                        //用户名已存在, 此处如何向用户反馈一个信息,且View中用户填的内容不会被清除。
                        ModelState.AddModelError("Account", "* 该账号已存在");

                        return View(t);
                    }else if (string.IsNullOrWhiteSpace(t.Account))
                    {
                        ModelState.AddModelError("Account", "账号不能为空");
                        return View(t);
                    }
                    else if(string.IsNullOrWhiteSpace(t.Name))
                    {                       
                        ModelState.AddModelError("Name", "姓名不能为空");
                        return View(t);
                    }
                  
                    if (ModelState.IsValid)
                    {                       
                        db.XXX.Add(t);
                        db.SaveChanges();
                    }
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View(t);
                }
            }
        }

猜你喜欢

转载自blog.csdn.net/ruoji_hui/article/details/84232982