asp.net-mvc设置文本框的值,使用 ASP.NET MVC 4 控制器

发布时间: 2016/10/9 2:32:48
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

它可以设置文本框的值,使用该控制器?我在我的视图中具有以下形式︰

@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 后我想要抓住的名字和姓氏的数据,从数据库,然后将其显示到更新或删除的视图。我试过 ViewBag、 可视数据和 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>
....

解决方法 1:

下面这行︰

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);
官方微信
官方QQ群
31647020