我有一个将生产者作为外键的电影模型。我希望在创建和编辑模型中能够在电影本身的“创建/编辑”视图中创建一个新的制作人。因此,我选择了html数据列表作为控件,可以在其中键入Producer Name或从现有Producers列表中进行选择。生产者模型的所有其他字段不是必需的。如何完成这样的解决方案?数据列表是我创建的自定义帮助程序-
public static HtmlString Datalist(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList)
{
//create the datalist tags
TagBuilder dl = new TagBuilder("datalist");
dl.Attributes.Add("id", name);
dl.Attributes.Add("Name",name);
// dl.Attributes.Add("class","form-control");
//create options List
List<string> options = new List<string>();
foreach (var d in selectList)
{
TagBuilder opt = new TagBuilder("option");
opt.Attributes.Add("value", d.Text);
options.Add(opt.ToString(TagRenderMode.StartTag));
}
string joineditems = string.Join("", options.ToArray());
joineditems += Environment.NewLine;
dl.InnerHtml = joineditems;
return new MvcHtmlString(dl.ToString());
}
它接收一个选择列表,并将其转换为数据列表。这是我的创建电影视图-
<div class="form-horizontal">
<h4>Movy</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Year_of_Release, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Year_of_Release, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Year_of_Release, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PLOT, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PLOT, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PLOT, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Poster, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Poster, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Poster, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">Producer</label>
<div class="col-md-10">
<input list="ProducerId" class="form-control"/>
@Html.Datalist("ProducerId", ViewBag.ProducerId as SelectList)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
ViewBag.ProducerId是包含生产者列表的选择列表。但是,输入的值未绑定到模型绑定器中。