【发布时间】:2016-02-21 15:46:51
【问题描述】:
我有一个这样的架构(保存在 Sql 中的表):
ID | Name | Address
1 Sam ADD1
2 John ADD2
3 Tony ADD3
我的表单中有三个字段 - ID , Name and Address。
现在,我使用ViewBag 为 ID 字段实现了一个下拉菜单,以在视图和控制器之间传递数据。
我想使用从下拉列表中选择的 ID 而不是手动设置字段 Name and Address 的值,但我无法找到方法并尝试搜索但找不到任何合适的解决方案。
感谢您的帮助。
编辑:
到目前为止我的代码:
模型已使用表本身生成(数据库优先方法)
控制器
public ActionResult Create()
{
ViewBag.codes = db.Users.ToList();
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Entity_Master entity_Master)
{
if (ModelState.IsValid)
{
db.Entity_Master.Add(entity_Master);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.codes = db.Users.ToList();
return View(entity_Master);
}
查看
@Html.LabelFor(model => model.ID, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.DropDownListFor(model => model.ID, new SelectList(ViewBag.codes, "ID", "ID", "Select ID"))
@Html.ValidationMessageFor(model => model.ID, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
呈现的 Html
<select id="ID" name="ID">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<input class="form-control text-box single-line" id="Name" name="Name" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="Name" data-valmsg-replace="true"></span>
<input class="form-control text-box single-line" id="Address" name="Address" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="Address" data-valmsg-replace="true"></span>
【问题讨论】:
-
请贴出你目前制作的代码
-
你需要发布渲染的html,我们没有办法处理你的MVC代码