1

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

3 Answers 3

2

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

}

Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

I did the code you gave above it seems fine but still no data is displayed in the textbox after i hit the search button.
Please post your view definition. Have you defined the @model to use in the view?
Yes I have already defined the @model in the view. Check my edit
Should work. Please show the updated post method. Have you verified that the model being passed to the view has its properties set?
1

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

3 Comments

On first load it says "Object reference not set to an instance of an object." It refers to this line: Line 42: <td>@Html.TextBoxFor(Models => Models.firstName, new { @value=Model.firstName, @class = "form-control", style = "width:380px;", disabled = "disabled" })</td>
Why use a TextBoxFor, and map it to the model, if you don't intend to use the model's value for that mapped field? It seems... redundant behavior to define the source if your information different from the property you map your textbox to.
@unique: I tried your solution to set a value in my textbox, however it didn't work. I looked for out there and I found this excellent article. Check it out: weblog.west-wind.com/posts/2012/Apr/20/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.