1

我是 asp.net MVC 的新手。在下面的代码中,我试图向数据库添加一条记录。它工作正常,但每次我加载视图时都会向数据库添加一条空记录。

模型:

 public class multi
    {
        public int CategoryID { get; set; }


        public string CategoryName { get; set; }
        public string Description { get; set; }

        public string DisplayName { get; set; }
        public string Email { get; set; }
    }



 public class CategoryRepository
    {

private BusDataClassesDataContext dc = new BusDataClassesDataContext();

 public void Save()
        {
            dc.SubmitChanges();
        }

         public void AddMulti(multi mu)
                {
                    tblCategory cat = new tblCategory();
                    cat.CategoryName = mu.CategoryName;
                    cat.Description = mu.Description;

                    tblUser user = new tblUser();
                    user.DisplayName = mu.DisplayName;
                    user.Email = mu.Email;

                    dc.tblCategories.InsertOnSubmit(cat);
                    dc.tblUsers.InsertOnSubmit(user);
                }
    }

控制器:

CategoryRepository cat = new CategoryRepository();

    public ActionResult AddMulti(multi mu)
            {
                if (ModelState.IsValid)
                {
                    cat.AddMulti(mu);
                    cat.Save();
                    return View(mu);

                }
                else
                {
                    return View(mu);

                }
            }

看法:

 <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>

        <fieldset>
            <legend>Fields</legend>


            <div class="editor-label">
                <%: Html.LabelFor(model => model.CategoryName) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.CategoryName) %>
                <%: Html.ValidationMessageFor(model => model.CategoryName) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Description) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Description) %>
                <%: Html.ValidationMessageFor(model => model.Description) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.DisplayName) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.DisplayName) %>
                <%: Html.ValidationMessageFor(model => model.DisplayName) %>
            </div>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.Email) %>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Email) %>
                <%: Html.ValidationMessageFor(model => model.Email) %>
            </div>

            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>

    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>
4

1 回答 1

1

这是因为您没有对模型进行验证,所以ModelState.IsValid总是如此。然后,您通过添加一个新类别AddMulti。因为这个控制器方法是唯一指定的,所以它同时用于 GET 和 POST 请求。这意味着,每次加载页面并呈现表单时,都会使用模型中的空值创建一条新记录。

尝试将控制器拆分为HttpPostHttpGet方法:

[HttpPost]
public ActionResult AddMulti(multi mu)
{
    if (ModelState.IsValid)
    {
        cat.AddMulti(mu);
        cat.Save();
        return View(mu);
    }
    else
    {
        return View(mu);
    }
}

[HttpGet]
public ActionResult AddMulti()
{
    return View();
}

现在,当您通过 GET 请求访问页面时(即,当您第一次访问它时,在发布表单之前),第二个操作方法被触发,因此您不会创建新记录。

您可能还想为模型添加一些验证属性。因为看起来您将域模型直接传递给视图,所以您还应该考虑创建单独的视图模型

于 2013-04-23T07:25:28.677 回答