在mvc实体框架中的下拉列表选择中填充文本框 [英] Fill textbox on dropdown selection in mvc entity framework
问题描述
我使用Asp.net MVC5和Entity Framework.I这两种技术都是新的。
基本上我创建一个表单,在这个表单中可以使用DropDown,当我从DropDown中选择值。我想填写这个表单上可用的文本框。
这是我的控制器
public class ChainController:Controller
{
private hcEntities db = new hcEntities();
// GET:Chain
public ActionResult Index()
{
ViewBag.name = new SelectList(db.chains,code,name) ;
return View(db.chains.ToList());
}
}
查看: - / p>
< div class =form-horizontal>
< hr />
< div class =form-group>
< label class =col-sm-2 control-label>
选择链
< / label>
< div class =col-md-3>
@ Html.DropDownList(name,null,new {@class =form-control})
< / div>
< / div>
@using(@ Html.BeginForm())
{
@ Html.AntiForgeryToken()
< div class =form-group>
< label class =col-sm-2 control-label>
Chain UserName
< / label>
< div class =col-md-3>
@ Html.TextBox(ChainName,null,new {@class =form-control})//我该怎么做
< / div>
< / div>
}
< / div>
模型(由EF生成的chain.cs)
public partial class chain
{
public long chain_id {get;组; }
public string name {get;组; }
public string code {get;组; }
public string username {get;组; }
public string password {get;组; }
public long created_by {get;组; }
public System.DateTime created_on {get;组; }
public Nullable< long> updated_by {get;组; }
public Nullable< System.DateTime> updated_on {get;组;
public chain()
{
created_by = 1;
created_on = DateTime.Now;
}
}
我不知道下一步会是什么。如何从选择下拉列表中填充用户名的文本框。
我在stackoverflow中找到太多的答案,但我填写这些对我没有帮助。例如
在mvc中选择下拉列表填充文本框,如何填写基于MVC中的下拉列表选择?
帮助我!
我自己做了。这是解决方案。
以前的代码中的一些更改
控制器 p>
而不是ViewBag,我使用ViewData。两个都是完美的,但我使用ViewData
// GET:链
public ActionResult Index()
{
ViewData [chain_name] = new SelectList(db.chains,code,name);
return View(db.chains.ToList());
}
另外我在控制器中创建一个从数据库中获取数据的功能
// Action函数
[HttpPost]
public ActionResult Action(string code)
{
var query = from c in db.chains
其中c.code == code
select c;
return Json(query);
}
之后,我通过javascript调用了这个Action Controller。 >
查看
@using(@Html。 BeginForm(Action,chains,FormMethod.Post))
{
@ Html.AntiForgeryToken()
< div class =form-horizontal>
< hr />
< div class =form-group>
< label class =col-sm-2 control-label>
选择链
< / label>
< div class =col-md-3>
@ Html.DropDownList(ddlchainname,(SelectList)ViewData [chain_name],new {onchange =Action(this.value);,@class =form-control})
< / div>
< / div>
< div class =form-group>
< label class =col-sm-2 control-label>
链名
< / label>
< div class =col-md-3>
@ Html.TextBox(ChainName,null,new {@class =form-control})
< / div>
< label class =col-sm-2 control-label>
用户名
< / label>
< div class =col-md-3>
@ Html.TextBox(username,null,new {@class =form-control})
< / div>
< / div>
< div class =form-group>
< label class =col-sm-2 control-label>
链码
< / label>
< div class =col-md-3>
@ Html.TextBox(ChainCode,null,new {@class =form-control})
< / div>
< / div>
< / div>
}
Javascript函数通过下拉列表onChange调用
< script type =text / javascript>
function Action(code){
$ .ajax({
url:'@ Url.Action(Action,Chains)',
type:POST ,
data:{code:code},
success:function(data){
if(data!= null){
var vdata = data; $ ($#ChainName); val(vdata [0] .code);
$(#ChainName #username)。val(vdata [0] .username);
}
}
});
}
< / script>
这很正常...
I am using Asp.net MVC5 and Entity Framework.I am new in Both of technologies.
Basically i create a Form, In this Form a DropDown available, when i select value from DropDown. I want to fill Textboxs which also available on this form.
This is my Controller
public class ChainController : Controller
{
private hcEntities db = new hcEntities();
// GET: Chain
public ActionResult Index()
{
ViewBag.name = new SelectList(db.chains,"code","name");
return View(db.chains.ToList());
}
}
View:-
<div class="form-horizontal">
<hr />
<div class="form-group">
<label class="col-sm-2 control-label">
Select Chain
</label>
<div class="col-md-3">
@Html.DropDownList("name" , null, new { @class = "form-control" })
</div>
</div>
@using (@Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-group">
<label class="col-sm-2 control-label">
Chain UserName
</label>
<div class="col-md-3">
@Html.TextBox("ChainName", null, new { @class = "form-control" }) //WHAT DO I DO HERE???????
</div>
</div>
}
</div>
Model(chain.cs generated by EF)
public partial class chain
{
public long chain_id { get; set; }
public string name { get; set; }
public string code { get; set; }
public string username { get; set; }
public string password { get; set; }
public long created_by { get; set; }
public System.DateTime created_on { get; set; }
public Nullable<long> updated_by { get; set; }
public Nullable<System.DateTime> updated_on { get; set; }
public chain()
{
created_by = 1;
created_on = DateTime.Now;
}
}
I don't know what will be next step. how to fill textbox with value of Username from the selection of dropdown. I find too many answer in stackoverflow but i fill these are not helpful for me. for example fill the textboxes with selecting dropdownlist in mvc, How to populate a textbox based on dropdown selection in MVC..?
Help Me !
I did it by my self. Here is the solution.
Some change in previous code
Controller
Instead of ViewBag, I use ViewData. Both are working perfectly, but i use ViewData
// GET: Chains
public ActionResult Index()
{
ViewData["chain_name"] = new SelectList(db.chains, "code", "name");
return View(db.chains.ToList());
}
Also i create a function in controller who fetch data from database
//Action Function
[HttpPost]
public ActionResult Action(string code)
{
var query = from c in db.chains
where c.code == code
select c;
return Json(query);
}
After that I call this Action Controller in my view through javascript.
View
@using (@Html.BeginForm("Action","chains", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
<div class="form-group">
<label class="col-sm-2 control-label">
Select Chain
</label>
<div class="col-md-3">
@Html.DropDownList("ddlchainname", (SelectList)ViewData["chain_name"], new { onchange = "Action(this.value);", @class = "form-control" })
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">
Chain Name
</label>
<div class="col-md-3">
@Html.TextBox("ChainName", null, new { @class = "form-control" })
</div>
<label class="col-sm-2 control-label">
Username
</label>
<div class="col-md-3">
@Html.TextBox("username", null, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">
Chain Code
</label>
<div class="col-md-3">
@Html.TextBox("ChainCode", null, new { @class = "form-control" })
</div>
</div>
</div>
}
Javascript Function call by dropdown onChange
<script type="text/javascript">
function Action(code) {
$.ajax({
url: '@Url.Action("Action", "Chains")',
type: "POST",
data: { "code": code },
"success": function (data) {
if (data != null) {
var vdata = data;
$("#ChainName").val(vdata[0].name);
$("#ChainCode").val(vdata[0].code);
$("#username").val(vdata[0].username);
}
}
});
}
</script>
That's work Fine...
这篇关于在mvc实体框架中的下拉列表选择中填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!