【发布时间】:2014-05-05 17:13:11
【问题描述】:
是否可以使用控制器设置文本框的值?我的视图中有以下表格:
@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 和 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>
....
【问题讨论】:
-
你在 stackoverflow stackoverflow.com/questions/9225039/…有这个问题的答案@
标签: asp.net-mvc asp.net-mvc-4 textbox