处理 MVC 表单提交到数据库

发布于 2024-12-22 14:34:54 字数 1810 浏览 1 评论 0原文

所以我大致遵循音乐商店教程。我在 pg.1 上创建了 StoreManagerController。 54左右。它创建了一个包含“创建”、“删除”、“编辑”等功能的视图。它将一些内容保存到数据库中,即我的 EditFor 控件,但没有其他内容。

我有多个 DropDownListFor 控件,由数据库中的表和 Active Directory 用户数据填充。我不知道如何保存这些。这是我的删节代码。感谢您的帮助。

视图:

<div class="createTopInner">
    <div class="editor-label">
        @Html.LabelFor(model => model.test.Category)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.CategoryId, Model.CategoryItems, "")
        @Html.ValidationMessageFor(model => model.test.Category)
    </div>
</div>

控制器:

public ActionResult Create()
{
    // These four lines get active directory users
    ActiveDirectoryModel ads = new ActiveDirectoryModel();
    ViewBag.assignedto = ads.FetchContacts();
    ViewBag.coassignedto = ads.FetchContacts();
    ViewBag.notifyto = ads.FetchContacts();
    var model = Populate();
    return View(model);
} 

[HttpPost]
public ActionResult Create(CreateViewModel model)
{
    if (ModelState.IsValid)
    {
        db.TestItems.AddObject(model.test);
        db.SaveChanges();
        return RedirectToAction("Index");  
    }
    return View(model);
}

public CreateViewModel Populate()
{
    var model = new CreateViewModel
    {
        CategoryItems =
            from c in new IntraEntities().CategoryItems.ToList()
            select new SelectListItem
            {
                Text = c.Name,
                Value = c.ID.ToString()
            }
    };
    return model;
}

型号:

public class CreateViewModel
{
    public Intra.Models.TestItem test{ get; set; }

    public int CategoryId { get; set; }
    public IEnumerable<SelectListItem> CategoryItems { get; set; }
}

So I'm loosely following the Music Store tutorial. I created the StoreManagerController on pg. 54ish. And it created a view with the Create, Deleted, Edit, etc. It's saving some stuff to the database, namely my EditFor controls, but nothing else.

I have multiple DropDownListFor controls, populated by both tables in the database and also Active Directory user data. I'm not sure how to get these to save. Here is my abridged code. Thanks for the help.

View:

<div class="createTopInner">
    <div class="editor-label">
        @Html.LabelFor(model => model.test.Category)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.CategoryId, Model.CategoryItems, "")
        @Html.ValidationMessageFor(model => model.test.Category)
    </div>
</div>

Controller:

public ActionResult Create()
{
    // These four lines get active directory users
    ActiveDirectoryModel ads = new ActiveDirectoryModel();
    ViewBag.assignedto = ads.FetchContacts();
    ViewBag.coassignedto = ads.FetchContacts();
    ViewBag.notifyto = ads.FetchContacts();
    var model = Populate();
    return View(model);
} 

[HttpPost]
public ActionResult Create(CreateViewModel model)
{
    if (ModelState.IsValid)
    {
        db.TestItems.AddObject(model.test);
        db.SaveChanges();
        return RedirectToAction("Index");  
    }
    return View(model);
}

public CreateViewModel Populate()
{
    var model = new CreateViewModel
    {
        CategoryItems =
            from c in new IntraEntities().CategoryItems.ToList()
            select new SelectListItem
            {
                Text = c.Name,
                Value = c.ID.ToString()
            }
    };
    return model;
}

Model:

public class CreateViewModel
{
    public Intra.Models.TestItem test{ get; set; }

    public int CategoryId { get; set; }
    public IEnumerable<SelectListItem> CategoryItems { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╄→承喏 2024-12-29 14:34:55

问题似乎是,虽然您的大多数输入映射到 test 上的属性,但 CategoryId 却没有。对您的实体模型一无所知,很难说,但我猜测您需要从数据库中检索相应的 Category 并将其添加到您的 TestItem 实例,然后再坚持它。如果您的 TestItem 实例上确实有一个 CategoryId 属性,您可以设置它,但我猜您没有这样做,因为否则您会直接使用它(就像您对 Category 标签所做的那样),而不是向视图模型添加属性。

The problem seems to be that, while most of your inputs map to properties on test, the CategoryId doesn't. Not knowing anything about your entity models, it's difficult to say, but I'd hazard a guess that you need to retrieve the corresponding Category from the database and add that to your TestItem instance before you persist it. If you do have a CategoryId property on your TestItem instance, you could just set it, but I'm guessing that you don't because otherwise you would have used it directly (as you do for the Category label) instead of adding a property to the view model.

山川志 2024-12-29 14:34:55

如果您有权访问它/对存储过程了解很多,那么最好在数据库中使用存储过程,然后在实体中调用它们。它的耦合更加松散,并且更容易进行更改,而无需重新编译代码。

If you have access to it / know much about stored procedures, it is much better to use Store procedures inside the database and then call them within Entity. It's much more loosely coupled and easier to make changes to without recompiling code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文