[英]Update Form Multiple Select Can't Display Value in ASP.NET MVC
I have a multiple select type control named fruit.我有一个名为fruit 的多选类型控件。 When I want to update the fruitcollection form, the value that I entered earlier cannot appear on the fruitcollection multiple select form.
当我想更新fruitcollection表单时,我之前输入的值无法出现在fruitcollection多选表单上。 What's the solution?
解决办法是什么? Thank you
谢谢
Update.cshtml (View) Update.cshtml(查看)
...
<select class="form-control select2" multiple="multiple" name="FruitsCollection" value="@Model.Fruit">
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Watermelon">Watermelon</option>
</select>
...
StoreController.cs (Controller) StoreController.cs(控制器)
MyDbContext db = new MyDbContext();
...
public ActionResult Update(int id)
{
MasterStore store = new MasterStore();
if (id!=0)
{
store = db.MasterStore.Where(s => s.Id == id).FirstOrDefault();
promo.FruitsCollection = promo.Fruit.Split(',').ToArray();
}
return View(store);
}
[HttpPost]
public ActionResult UpdateStore(MasterStore store)
{
MasterStore p = db.MasterStore.Where(s => s.Id == store.Id).First();
p.Id = store.Id;
p.Fruit = store.Fruit;
}
...
MasterStore.cs (Model) MasterStore.cs(模型)
...
public partial class MasterStore
{
public int Id { get; set; }
public string Fruit { get; set; }
[NotMapped]
public string[] FruitsCollection { get; set; }
}
...
Based on your model, you could do something like this:根据您的模型,您可以执行以下操作:
<select class="form-control select2" multiple="multiple" name="FruitsCollection">
<option value="Apple" selected="@(Model.FruitsCollection.Contains("Apple") ? "selected" : null)">Apple</option>
<option value="Banana" selected="@(Model.FruitsCollection.Contains("Banana") ? "selected" : null)">Banana</option>
<option value="Watermelon" selected="@(Model.FruitsCollection.Contains("Watermelon") ? "selected" : null)">Watermelon</option>
</select>
This part,这部分,
selected="@(Model.FruitsCollection.Contains("Apple") ? "selected" : null)"
will either render as selected="selected"
, or be omitted - the tag builder omits attributes that are set to null
.将呈现为
selected="selected"
,或者被省略 - 标签构建器会忽略设置为null
属性。
That's not really going to scale well.这不会很好地扩展。 You probably would get better results from using the
HtmlHelper
for a multi-select, the ListBoxFor helper:使用
HtmlHelper
进行多选, ListBoxFor助手可能会获得更好的结果:
@Html.ListBoxFor(m => m.FruitCollection,
/* todo */,
new { @class="form-control select2" })
You would need to decide how you want to build the required second parameter, where I have the /* todo */
placeholder.您需要决定如何构建所需的第二个参数,其中我有
/* todo */
占位符。 That needs to be an instance of the MultiSelectList class, which you can create inline or as part of creating the model within the controller.这需要是MultiSelectList类的一个实例,您可以创建内联或作为在控制器内创建模型的一部分。 Here's the bare minimum to do it inline with your values:
这是与您的价值观一致的最低限度:
@Html.ListBoxFor(m => m.FruitCollection,
new MultiSelectList(new string[]{ "Apple", "Banana", "Watermelon" }),
new { @class="form-control select2" })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.