asp.net - DropDownlistFor with a null-value as default -
asp.net - DropDownlistFor with a null-value as default -
the code below displays 1 dropdownlist categories , empy textbox creating new category. both "target" same property , hope next sceanrio: if user have not made selection in dropdownlistfor
, value in textboxfor
should passed controller.
my problem there value selected in dropdownlist default. there way have default value ---choose category---, has value of null , not passed controller?
@using (html.beginform("savecategory", "home", formmethod.post)) { <div class="col-md-3"> @html.hiddenfor(o => o.id) <p>choose existing category</p> @html.dropdownlistfor(model => model.choosencategory, model.category.listofcategories.select(kat => new selectlistitem { text = kat, value = kat })) </div> <div class="col-md-3"> <p>create new category</p> @html.textboxfor(o => o.blogcomment.combody, new { @class = "form-control", placeholder = "add new category" }) </div> <div class="col-md-3"> <button type="submit" name="xid" class="btn btn-secondary" style="margin-top: 30px;">save category</button> </div> <div style="float: none;clear: both"></div> }
set property drop downwards -- select category --
something :
@html.dropdownlistfor(model => model.choosencategory, model.category.listofcategories.select(kat => new selectlistitem { text = kat, value = kat }), "-- select category --")
and allow me know if works you.
asp.net
Comments
Post a Comment