c# - Returning int value from @html.DropDownList -
actually have problem happened because of lack of knowledge in asp.net - mvc 4. trying getting selected value drop down , return controller (as integer). makes me more confuse other dropdownlist returning string value working fine.
my model :
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; } }
my controller : //populating viewbag
listproductcategory = new list<product_category>(); listproductcategory = db.product_category.tolist(); ilist<product_category> prodcategories = db.product_category.tolist(); ienumerable<selectlistitem> selectlistcategory = c in prodcategories select new selectlistitem { text = c.category_name, value = c.id.tostring() }; viewbag.productcategorydata = selectlistcategory;
//create action
[httppost] public actionresult create(product product) { if (modelstate.isvalid) { db.products.add(product); db.savechanges(); return redirecttoaction("index"); } return view(product); }
my view :
<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>
the dropdown lsit populated value of productcategories not being returnd, returning 0 instead of value.
please ..
what other dropdownlists returning strings? bet string returning part of model. model doesn't have productcategories
property. try changing:
@html.dropdownlist("productcategories", new selectlist(viewbag.productcategorydata, "value", "text"))
to:
@html.dropdownlist("category_id", new selectlist(viewbag.productcategorydata, "value", "text"))
Comments
Post a Comment