【问题标题】:Returning int value from @html.DropDownList从 @html.DropDownList 返回 int 值
【发布时间】:2013-05-21 03:17:41
【问题描述】:

实际上,由于我对 ASP.NET - MVC 4 缺乏了解,我遇到了一些问题。 我要做的是从下拉列表中获取选定的值并将其返回给控制器(作为整数)。让我更困惑的是另一个返回字符串值的 DropDownList 工作正常。

我的模特:

public partial class PRODUCT
{
    public int ID { get; set; }
    public string PRODUCT_NAME { get; set; }
    public int CATEGORY_ID { get; set; }
    public string LANGUAGE { get; set; }
}

public partial class PRODUCT_CATEGORY
{
    public int ID { get; set; }
    public string CATEGORY_NAME { get; set; }
    public string LANGUAGE { get; set; }
}

我的控制器: //填充ViewBag

ListProductCategory = new List<PRODUCT_CATEGORY>();
ListProductCategory = db.PRODUCT_CATEGORY.ToList();

IList<PRODUCT_CATEGORY> prodCategories = db.PRODUCT_CATEGORY.ToList();
IEnumerable<SelectListItem> selectListCategory =
     from c in prodCategories
     select new SelectListItem
     {
      Text = c.CATEGORY_NAME,
       Value = c.ID.ToString()
     };

ViewBag.ProductCategoryData = selectListCategory;

//创建动作

[HttpPost]
public ActionResult Create(PRODUCT product)
{
    if (ModelState.IsValid)
    {
        db.PRODUCTs.Add(product);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(product);
}

我的观点:

<div class="editor-label">
        @Html.LabelFor(model => model.CATEGORY_ID)
 </div>

 <div class="editor-field">            
   @Html.DropDownList("ProductCategories", new  SelectList(ViewBag.ProductCategoryData, "Value", "Text"))          
   @Html.ValidationMessageFor(model => model.CATEGORY_ID)
 </div>

DropDown lsit 已填充,但未返回 ProductCategories 的值,它返回 0 而不是值。

请帮忙..

【问题讨论】:

    标签: c# asp.net-mvc razor html-select


    【解决方案1】:

    还有哪些其他下拉列表返回字符串?我敢打赌他们返回的字符串是模型的一部分。您的模型没有 ProductCategories 属性。尝试改变:

    @Html.DropDownList("ProductCategories", new  SelectList(ViewBag.ProductCategoryData, "Value", "Text"))          
    

    收件人:

    @Html.DropDownList("CATEGORY_ID", new  SelectList(ViewBag.ProductCategoryData, "Value", "Text"))          
    

    【讨论】:

    • 它有效!多谢。起初我认为第一个参数中的名称仅供参考,所以我只是放了我喜欢的任何东西。看起来这个 ASP.NET 对我来说还有很长的路要走。百万感谢您的帮助。 :-)
    【解决方案2】:

    试试看

    在模型中添加额外的属性

    public partial class PRODUCT
    {
        public int ID { get; set; }
        public string PRODUCT_NAME { get; set; }
        public int CATEGORY_ID { get; set; }
        public string LANGUAGE { get; set; }
        **public int SelectedCATEGORY_ID { get; set; }**
    }
    
    
    @Html.DropDownListFor(**model=>model.SelectedCATEGORY_ID** , new  SelectList(ViewBag.ProductCategoryData, "Value", "Text"))  
    

    现在您可以在 SelectedCATEGORY_ID 属性中获取下拉选择值。


     @Html.DropDownListFor(**model=>model.CATEGORY_ID ** , new  SelectList(ViewBag.ProductCategoryData, "Value", "Text"))  
    
    
        Now you can get the dropdown selected value in the CATEGORY_ID property .
    

    【讨论】:

    • 模型已更新,HtmlDropDownList 已修改,但我收到一条错误消息,提示“无法将 lambda 表达式转换为类型‘字符串’,因为在应用 Html 部分时它不是委托类型。我应该更改其他内容吗?
    • 将 DropDownList 更改为 DropDownListFor
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多