C# 如何根据MVC中的下拉选择填充文本框..?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16814450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to populate a textbox based on dropdown selection in MVC..?
提问by RajeshKannan
Hi I have created a table and connected it to MVC project through ADO.NET entity. After connecting I added the controller for the entity and it creates a set of cshtml files in VIEW folder in MVC project. But now What I need is to create a dropdownlist and textbox. I created the dropdownlist in a cshtml file and also wriiten the logic for it in the CONTROLLER. I can also create TEXTBOXES,but i'm facing the problem of poulating TEXTBOX based on the dropdownlist selection.
嗨,我创建了一个表并通过 ADO.NET 实体将它连接到 MVC 项目。连接后,我为实体添加了控制器,它在 MVC 项目的 VIEW 文件夹中创建了一组 cshtml 文件。但现在我需要的是创建一个下拉列表和文本框。我在 cshtml 文件中创建了下拉列表,并在 CONTROLLER 中编写了它的逻辑。我也可以创建 TEXTBOXES,但我面临着根据下拉列表选择填充 TEXTBOX 的问题。
My Model auto generated by VS 2012 is
我由 VS 2012 自动生成的模型是
public partial class Plan_S
{
public int PlanId_PK { get; set; }
public string PlanNames { get; set; }
public string Hours { get; set; }
}
My Controller for displaying dropdownlist is `
我的用于显示下拉列表的控制器是 `
public class dropdownController : Controller
{
private PivotEntities db = new PivotEntities();
//
// GET: /dropdown/
public ActionResult Index()
{
ViewBag.plannames = new SelectList(db.Plan_S, "PlanId_PK", "PlanNames");
return View();
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
public ActionResult ddl()
{
return View(new Plan_S());
}
}`
My view.cshtml for displaying dropdownlist is
我用于显示下拉列表的 view.cshtml 是
`
`
@model Pivot.Models.Plan_S
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div>
@Html.DropDownList("PlanNames", "--select--")
</div>
`
`
Now When I select an item in the dropdownlist, it should automatically populate the corresponding value in the table. Here in my code, Plan_S table is autogenrated as Plan_S MODEL Class. But in Database I have set of values for these columns in table.
现在,当我在下拉列表中选择一个项目时,它应该会自动填充表中的相应值。在我的代码中,Plan_S 表自动生成为 Plan_S MODEL 类。但是在数据库中,我为表中的这些列设置了一组值。
eg..) PlanId_PK | PlanNames | Hours
1 Plan1 1hrs
2 Plan2 2hrs
3 Plan3 3hrs
Here in this Plan_S table,
在这个 Plan_S 表中,
PlanNames column is populated in the DROPDOWNLIST, When I select the Plan1 in DDL it should populate the texbox as 1hrs
PlanNames 列填充在 DROPDOWNLIST 中,当我在 DDL 中选择 Plan1 时,它应该将 texbox 填充为 1hrs
When I select the Plan2 in DDL it should populate the textbox as 2hrs.
当我在 DDL 中选择 Plan2 时,它应该将文本框填充为 2hrs。
This is the logic i need and I can do this in asp webforms but it is tricky in MVC.
这是我需要的逻辑,我可以在 asp webforms 中做到这一点,但在 MVC 中很棘手。
I think that Jquery is needed for it.......
我认为它需要 Jquery ......
Please help me, I had spent hours in finding this logic....
请帮助我,我花了几个小时来寻找这个逻辑......
Thanks in advance...
提前致谢...
采纳答案by mattytommo
Firstly, let's create a View Model to hold these things:
首先,让我们创建一个视图模型来保存这些东西:
public class PlanViewModel
{
public List<SelectListItem> Plans { get; set; }
}
Then, in your controller action let's build the Model:
然后,在您的控制器操作中,让我们构建模型:
public ActionResult Index()
{
var model = new PlanViewModel();
model.Plans = db.Plan_S
.Select(p => new SelectListItem
{
Value = p.Hours,
Text = p.PlanNames
})
.ToList();
return View(model);
}
Then in your View, do:
然后在您的视图中,执行以下操作:
@model Pivot.Models.Plan_S
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div>
@Html.DropDownList("PlanNames", Model.Plans, "--select--")
<input id="planHours" type="text" />
</div>
Then you'll need to do the following in jQuery:
然后你需要在 jQuery 中执行以下操作:
<script type="text/javascript">
$(function () {
$("[name='PlanNames']").change(function () {
$("#planHours").val($(this).val());
});
});
</script>

