Working on my first MVC ASP.NET project. I am wondering how I can update the model using user's inputs.
Let's say I have a model PersonModel.cs
public class PersonModel
{
int personID;
string firstName;
string lastName;
string favoriteColor;
public PersonModel(int personID){
this.PersonID=personID;
//..Fetching data from DB to fill firstName and lastName using personID
//At this point, personID,firstName and lastName are set, only the favoriteColor is missing
}
}
Now my controller:
public class PersonController : Controller
{
public ActionResult Index() {
PersonModel person = new PersonModel(this.UserId());
return View(person);
}
In my View :
@using Portail.Models
@model PersonModel
@ViewBag.Title = "Welcome page"
<h2> Hello @Model.firstName @Model.lastName </h2>
For now it's working fine, displaying the first and last names of the user, according to the values in the DB.
I would like the user to choose a color in a list and then set this color as the favoriteColor variable in the model, and then update the DB with it. For example, by adding a drop down list:
<select>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="yellow">Yellow</option>
</select>
How can I update my model by setting the chosen color for favoriteColor ?
Note that this is just an example, for the project I am working on I need to update a couple of variables, not just one like it's the case here. Also, it's confidential data so I don't want to pass anything in URL.