asp.net-mvc 使用控制器 ASP.NET MVC 4 设置文本框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23464480/
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
Set the textbox value using the controller ASP.NET MVC 4
提问by Nate
Is it possible to set the value of the textbox using the controller? I have the following form in my View:
是否可以使用控制器设置文本框的值?我的视图中有以下表格:
@Html.TextBoxFor(Models => Models.techNo, new { @class="form-control maintain-text",
placeholder="Technician No..."})
<button type="submit" id="search" name="SubmitButton" value="search" class="btn btn-default">Search</button>
<td>First Name :</td>
<td>@Html.TextBoxFor(Models => Models.firstName, new { @class = "form-control", style = "width:380px;", disabled = "disabled" })</td>
<td>Last Name :</td>
<td>@Html.TextBoxFor(Models => Models.lastName, new { @class = "form-control", style = "width:380px;", disabled = "disabled" })</td>
Model:
模型:
public string techNo { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
Controller:
控制器:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult ProcessTech(string SubmitButton)
{
TechnicianFacade _oTechFacade = new TechnicianFacade();
Technician _oTechnician = new Technician();
string fName = Request.Form["firstName"].ToString().ToUpper();
string lName = Request.Form["lastName"].ToString().ToUpper();
switch (SubmitButton)
{
case "save": //add new technician
{
}
break;
case "update": //update technician details
{
}
break;
case "search": //search technician
{
try {
string id = Request.Form["techNo"].ToString().ToUpper();
if (isValid(id, "technician") == false) //search the id from the database
{
//do nothing
}
else //if id is valid
{
var tech = _oTechFacade.getTechnicians(id, _oAppSetting.ConnectionString)[0]; //data from the database
string fNameStr = tech.GetType().GetProperty("FIRSTNAME").GetValue(tech, null).ToString();
string lNameStr = tech.GetType().GetProperty("LASTNAME").GetValue(tech, null).ToString();
//When the the ID is found,
//these are the values of the firstName & lastName that I want to set to the View
//Is it possible to set the values of the textboxes from here?
}
}
catch (Exception ex) { throw ex; }
}
break;
case "delete":
{
}
break;
}
return View("Index");
}
Basically, after searching the ID(assuming its valid) I want to grab the firstName & lastName data from the database and then display it to the View for updating or deleting. I've tried ViewBag, ViewData, and TempData but all are not working for me.
基本上,在搜索 ID(假设其有效)后,我想从数据库中获取 firstName 和 lastName 数据,然后将其显示到视图中以进行更新或删除。我已经尝试过 ViewBag、ViewData 和 TempData,但都不适用于我。
EDIT:
编辑:
@model Maintenance_.Models.IndexModel
@{
ViewBag.Title = "Technician";
}
<div id="page-wrapper">
<div class = "row">
<div class = "col-lg-12">
<h1 class= "page-header"> Technician </h1>
</div>
</div>
....
采纳答案by Brendan Green
This line:
这一行:
return View("Index");
is not passing a model to the Index view.
没有将模型传递给索引视图。
You need to create an instance of your view, and set its properties:
您需要创建视图的实例,并设置其属性:
var model = new IndexModel();
...
else //if id is valid
{
var tech = _oTechFacade.getTechnicians(id, _oAppSetting.ConnectionString)[0]; //data from the database
string fNameStr = tech.GetType().GetProperty("FIRSTNAME").GetValue(tech, null).ToString();
string lNameStr = tech.GetType().GetProperty("LASTNAME").GetValue(tech, null).ToString();
//When the the ID is found, we populate our model
//so it can be displayed
model.firstname = fNameStr;
model.lastname = lNameStr;
}
Now you can do:
现在你可以这样做:
return View("Index", model);
回答by Ravindra Kumar
As i see your code, you are using explicitly typed view and using same view for insert and update. And in this case you must return model to your view like that:
正如我看到您的代码,您正在使用显式类型的视图并使用相同的视图进行插入和更新。在这种情况下,您必须像这样将模型返回到您的视图:
[HttpPost]
public ActionResult ProcessTech(string SubmitButton)
{
TechnicianFacade _oTechFacade = new TechnicianFacade();
Technician _oTechnician = new Technician();
string fName = Request.Form["firstName"].ToString().ToUpper();
string lName = Request.Form["lastName"].ToString().ToUpper();
YourModelName model = new YourModelName ();
switch (SubmitButton)
{
case "save": //add new technician
{
}
break;
case "update": //update technician details
{
}
break;
case "search": //search technician
{
try {
string id = Request.Form["techNo"].ToString().ToUpper();
if (isValid(id, "technician") == false) //search the id from the database
{
//do nothing
}
else //if id is valid
{
var tech = _oTechFacade.getTechnicians(id, _oAppSetting.ConnectionString)[0]; //data from the database
string fNameStr = tech.GetType().GetProperty("FIRSTNAME").GetValue(tech, null).ToString();
string lNameStr = tech.GetType().GetProperty("LASTNAME").GetValue(tech, null).ToString();
model.firstname = fNameStr;
model.lastname = lNameStr;
//When the the ID is found,
//these are the values of the firstName & lastName that I want to set to the View
//Is it possible to set the values of the textboxes from here?
}
}
catch (Exception ex) { throw ex; }
}
break;
case "delete":
{
}
break;
}
return View("Index",model);
}
}
回答by unique
@Brendan Green answer is correct you need pass model to the view Also then you can set text box value using add new property to Html Helper.
@Brendan Green 答案是正确的,您需要将模型传递给视图,然后您可以使用向 Html Helper 添加新属性来设置文本框值。
@Html.TextBoxFor(Models => Models.firstName, new { @class = "form-control", style = "width:380px;", disabled = "disabled", @value = Model.firstName })
Note here value set by @value property
注意这里由@value 属性设置的值
Update:
更新:
Change < button > element to < input type="submit" > also change the id to SubmitButton and Name also SubmitButton (id and Name values same is good)then try again seems to problem with your switch statement. Debug it then you can see what happen. Also pass model to view and Name of the View should be Index then view correctly calling.
将 <button> 元素更改为 <input type="submit"> 还将 id 更改为 SubmitButton 和 Name 也将 SubmitButton(id 和 Name 值相同是好的)然后再试一次似乎您的 switch 语句有问题。调试它然后你可以看到发生了什么。还将模型传递给视图,视图的名称应该是索引,然后正确调用视图。

