Pregunta

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.

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>
....
¿Fue útil?

Solución 2

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

Otros consejos

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

}

@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.

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

Note here value set by @value property

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top