是否可以使用控制器设置文本框的值?我在查看:
中有以下表单@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>
型号:
public string techNo { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
控制器:
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");
}
基本上,在搜索ID后(假设它有效)我想抓住firstName&amp;来自数据库的lastName数据,然后将其显示给View以进行更新或删除。我已经尝试过ViewBag,ViewData和TempData,但所有这些都不适合我。
修改
@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>
....
答案 0 :(得分:1)
在我看到您的代码时,您正在使用显式类型视图并使用相同的视图进行插入和更新。在这种情况下,您必须将模型返回到您的视图中:
[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);
}
答案 1 :(得分:0)
这一行:
return View("Index");
未将模型传递到索引视图。
您需要创建视图的实例,并设置其属性:
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;
}
现在你可以做到:
return View("Index", model);
答案 2 :(得分:0)
@Brendan绿色答案是正确的你需要将模型传递给视图然后你可以使用向Html Helper添加新属性来设置文本框值。
@Html.TextBoxFor(Models => Models.firstName, new { @class = "form-control", style = "width:380px;", disabled = "disabled", @value = Model.firstName })
请注意@value属性设置的值
<强>更新强>
更改&lt;按钮&gt;元素到&lt; input type =“submit”&gt;也将id改为SubmitButton,Name也改为SubmitButton(id和Name值一样好)然后再试一次看起来你的switch语句有问题。调试它然后你可以看到发生了什么。 同时将模型传递给视图,视图名称应为索引,然后正确查看。