具有类型列表模型的mvc下拉列表 [英] mvc Dropdown list with model of type list

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

问题描述

我对MVC还是很陌生,但是我一直在尝试在具有类型为List<>的模型的视图中使用下拉列表.这样做的原因是在单个页面上创建多个记录.

I am pretty new to MVC however I have been trying to use a dropdownlist in a view which has a model of type List<>. The reason for this is to create multiple records on a single page.

我能够一次成功创建一个非List类型的视图,并且一次没有一个记录,但是当我使用以下模型实现视图时,尽管进行了大量的搜索仍无法实现解决方案键入List<>.

I have been able to successfully create a view that is not of type List<> and a single record at a time, but in spite of a lot of googling can not implement the solution when I implement the view with a model of type List<>.

型号

public class Product
{
    public int ProductID { get; set; }

    public string Description { get; set; }

    public int VatRateID { get; set; }

    [ForeignKey("VatRateID")]
    public virtual VatRate VatRate { get; set; }

}

控制器

    // GET: /Product/Bulkdata
    public ActionResult BulkData()
    {
        PopulateVatRateDropDownList();
        List<Product> model = new List<Product>();
        model.Add(new Product
                        {
                            ProductID = 0,
                            Description = "",
                            VatRateID = 1
                        }
                );
        return View(model);
    }

    //
    // POST: /Product/Bulkdata

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult BulkData(
        [Bind(Include = "Description,VatRateID")]
        List<Product> products)
    {

        try
        {
            if (ModelState.IsValid)
            {
                foreach (var p in products)
                {
                    db.Product.Add(p);
                }
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }
        catch (DataException)
        {
            ModelState.AddModelError("", "Bulk - Unable to save changes, Try again, and if the probelm persists contact your administrator");
        }
        PopulateVatRateDropDownList();
        return View(products);
    }

    private void PopulateVatRateDropDownList(object selectedVatRate = null)
    {
        var vatRateQuery = from v in db.VatRate
                           select v;
        ViewBag.VatRateID = new SelectList(vatRateQuery, "VatRateID", "VatRateDescription", selectedVatRate);
    }

查看

   @if (Model != null && Model.Count > 0)
   {
       int j = 0;
       foreach (var i in Model)
       {

            <tr style="border:1px solid black">
                <td>
                    @Html.EditorFor(m => m[j].Description)
                    @Html.ValidationMessageFor(m => m[j].Description)
                </td>
                <td>
                    @Html.DropDownList("VatRateID", String.Empty)
                    @Html.ValidationMessageFor(m => m[j].VatRateID)
                </td>
                <td>
                   @if (j > 0)
                   {
                        <a href="#" class="remove">Remove</a>
                   }
                </td>
            </tr>
            j++;
       }
   }

当我使用此代码运行应用程序时,则0总是回到vatRateID.除上述以外,我还尝试了许多其他解决方案,包括类型为List< string>的ASP.NET MVC DropDownListFor .正如我提到的那样,我是mvc的新手,所以我知道可能缺少一些简单的东西.任何帮助将不胜感激.

When I run the application with this code then 0 is always past back to vatRateID. I have tried numerous other solution other than the above including ASP.NET MVC DropDownListFor with model of type List<string>. As I have mentioned I am new to mvc so I know there is probably something simple that I am missing. Any help would be hugely appreciated.

谢谢

推荐答案

斯蒂芬(Stephen)对此进行了回答:

Answered by Stephen in comments:

在视图中,按照Stephen评论中的文章将foreach更改为for循环,并使用DropdDownListFor

In the view, changed the foreach to a for loop as per the article in Stephen's comment and used DropdDownListFor

<代码>@ Html.DropDownListFor(m => m [i] .VatRateID,(IEnumerable< SelectListItem>)ViewBag.VatRateList)

还修改了控制器,将ViewBag属性重命名为VatRateList

Also amended the controller to rename the ViewBag property to VatRateList

'私人无效PopulateVatRateDropDownList(object selectedVatRate = null){var vatRateQuery =来自db.VatRate中的v选择v;ViewBag.VatRateID =新的SelectList(vatRateQuery,"VatRateID","VatRateDescription",selectedVatRate);}'

' private void PopulateVatRateDropDownList(object selectedVatRate = null) { var vatRateQuery = from v in db.VatRate select v; ViewBag.VatRateID = new SelectList(vatRateQuery, "VatRateID", "VatRateDescription", selectedVatRate); } '

这篇关于具有类型列表模型的mvc下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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