【问题标题】:Set the textbox value using the controller ASP.NET MVC 4使用控制器 ASP.NET MVC 4 设置文本框值
【发布时间】: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>
....

【问题讨论】:

标签: asp.net-mvc asp.net-mvc-4 textbox


【解决方案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);

}

【讨论】:

    【解决方案2】:

    这一行:

    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);
    

    【讨论】:

    • 我做了你上面给出的代码,看起来不错,但是在我点击搜索按钮后,文本框中仍然没有显示数据。
    • 请发布您的视图定义。您是否定义了要在视图中使用的 @model?
    • 是的,我已经在视图中定义了@model。检查我的编辑
    • 应该可以。请显示更新后的发布方法。您是否验证过传递给视图的模型已设置其属性?
    【解决方案3】:

    @Brendan Green 的回答是正确的,您需要将模型传递给视图,然后您可以使用向 Html Helper 添加新属性来设置文本框值。

    @Html.TextBoxFor(Models => Models.firstName, new { @class = "form-control", style = "width:380px;", disabled = "disabled", @value = Model.firstName  })
    

    注意这里的值由@value 属性设置

    更新:

    【讨论】:

    • 第一次加载时显示“对象引用未设置为对象的实例。”它指的是这一行: 第 42 行:@Html.TextBoxFor(Models => Models.firstName, new { @value=Model.firstName, @class= "form-control", style = "width:380px;" , 禁用 = "禁用" })
    • 如果您不打算将模型的值用于该映射字段,为什么要使用 TextBoxFor 并将其映射到模型?似乎......如果您的信息与您将文本框映射到的属性不同,则定义源的行为是多余的。
    • @unique:我尝试了您的解决方案来在我的文本框中设置一个值,但是它不起作用。我在那里寻找并找到了这篇优秀的文章。看看:weblog.west-wind.com/posts/2012/Apr/20/…
    猜你喜欢
    • 2013-05-23
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    相关资源
    最近更新 更多