在下拉列表编辑MVC3形式 [英] dropdown in mvc3 edit form

查看:213
本文介绍了在下拉列表编辑MVC3形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这也许很简单,但我似乎无法梳理出来我自己。
我创建了一个简单的DB和实体模式,看起来像这样

This maybe very simple but I cant seem to sort it out on my own. I have created a simple db and entity modal that looks like this

我想创建一个创建表格,让我补充一个新的秩序。我一共3个表,所以我试图做的是有形式允许人进入订单日期,也有一个下拉列表,让我选择从产品表中的产品

I am trying to create an Create form that allows me to add a new Order. I have a total of 3 tables so what I am trying to do is have the form allowing the person to enter Order date and also has a dropdown list that allows me to select a product from the product table

我希望能够创建一个让我订购日期插入OrderTable也插入OrderID和选择的产品ID为Or​​derProduct一个添加或编辑视图。

I want to be able to create a Add or Edit view that allow me to insert the OrderDate into the OrderTable and also insert the OrderID and selected ProductID into OrderProduct.

哪些步骤,我需要在这里做。

What steps do I need to do here.

我已经创建了一个OrderController并勾选添加操作,比增加了创建视图它看起来像这样

I have created an OrderController and ticked the "Add Actions" and than added a Create View which looks like this

@model Test.OrderProduct

@{
    ViewBag.Title = "Create2";
}

    <h2>Create2</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>OrderProduct</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.OrderID)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.OrderID)
            @Html.ValidationMessageFor(model => model.OrderID)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ProductID)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ProductID)
            @Html.ValidationMessageFor(model => model.ProductID)
        </div>

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

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

这将创建一个包含两个订单ID和产品ID的文本框却没有日期的看法。

This creates the view that contains a textbox for both OrderID and ProductID however no date.

我的控制器CreatePost处理不当被修改

My controller CreatePost hasnt been changed

  [HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        try
        {
            var data = collection;
            // TODO: Add insert logic here
          //  db.Orders.AddObject(collection);
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

我的问题是,

1.How我换出的ProductID文本框是是从产品填充下拉
2.How我得到的FormCollection收集的数据?我以为只是一个foreach的,但是我不知道怎么弄的强类型名称

1.How do I swap out ProductID textbox to be a dropdown which is populated from Product 2.How do I get the data from FormCollection collection? I thought of just a foreach however I dont know how to get the strongly typed name

对于一个新手任何帮助将是非常有益的。

Any help for a newbie would be very helpful.

感谢您!

推荐答案

第一件事,第一,不绑定在订单实体。千万不要绑定到EF对象,总是试图用的视图模型。使得视野中的生活比较简单,那就是这里的目标。

First thing's first, don't bind to the Order entity. Never bind to an EF object, always try and use a ViewModel. Makes life simpler for the View, and that is the goal here.

所以,有这样的视图模型:

So, have a ViewModel like this:

public class CreateOrderViewModel
{
   public int OrderId { get; set; }
   public DateTime OrderDate { get; set; }
   public int SelectedProductId { get; set; }
   public IEnumerable<SelectListItem> Products { get; set; }
}

这就是它现在。

返回,为您的查看你的 [HTTPGET] 控制器动作:

Return that to your View in your [HttpGet] controller action:

[HttpGet]
public ActionResult Create()
{
   var model = new CreateOrderViewModel
   {
      Products = db.Products
                   .ToList() // this will fire a query, basically SELECT * FROM Products
                   .Select(x => new SelectListItem
                    {
                       Text = x.ProductName,
                       Value = x.ProductId
                    });
   };

   return View(model);
}

然后呈现出产品的名单:(基本的HTML除外)

Then to render out the list of Products: (basic HTML excluded)

@model WebApplication.Models.CreateOrderViewModel

@Html.DropDownListFor(model => model.SelectedProductId, Model.Products)

我不知道该怎么做的唯一事情就是绑定到DateTime字段。我猜你会需要它呈现出的日期选择器什么的一个扩展方法(HTML帮助)。该视图(创建新订单),就默认为 DateTime.Now

现在,到 [HttpPost] 控制器动作:

[HttpPost]
public ActionResult Create(CreateOrderViewModel model)
{
   try
   {
      // TODO: this manual stitching should be replaced with AutoMapper
      var newOrder = new Order
      {
         OrderDate = DateTime.Now,
         OrderProduct = new OrderProduct
         {
            ProductId = SelectedProductId
         }
      };

      db.Orders.AddObject(newOrder);
      return RedirectToAction("Index");
   }
   catch
   {
      return View();
   }
}

现在,我也觉得你的EF模型需要的工作。

Now, i also think your EF model needs work.

要我(英文术语),一个产品可以有多个订单,一个订单可以有很多产品。​​

因此​​,它应该是一个多对许多。目前,它是一个1-1冗余连接表。你有没有产生来自DB?如果是这样,你可能DB需要的工作。

So, it should be a many-to-many. Currently it's a 1-1 with a redundant join table. Did you generate that from a DB? If so, your DB possibly needs work.

您应该有一个导航属性​​时调用的产品订单实体,它引用的产品的集合,通过无声成为可能加盟在连接表许多一对多。

You should have a navigational property called Products on the Order entity, which references a collection of Product, made possible by a silent join to the join table in the many-to-many.

这也意味着你不再有一个DropDownList,而是一个MultiSelectDropDownList。

This also means you no longer have a DropDownList, but a MultiSelectDropDownList.

这篇关于在下拉列表编辑MVC3形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆