Reputation: 157
I have a table which stores Person. The attributes are FirstName
, LastName
and PersonID
. I have added the attribute
public string NameFull { get { return FirstName + " " + LastName; } }
and I am able to display the output as a concatenated name.
However, when the user makes the selection of the name, I want to store it as the PersonID.
Currently, I have this as my linq Where statement:
var personID= db.Persons
.Where(h => Names == model.Person.NameFull)
.Select(h => h.PersonID);
var idToSave = personID.AsEnumerable().FirstOrDefault().Trim().ToString();
In the controller:
SelectList names = new SelectList(db.Person.ToArray(), "PersonID", "NameFull");
ViewBag.Names = names;
Would anyone be able to point me in the right direction?
Upvotes: 1
Views: 3127
Reputation: 141712
Here is a running fiddle that I created for you. Select a person and click submit. The selected person's id will display after the HTTP post.
MyViewModel
contains a List
of persons and a SelectedPersonID
. It is this view model that we bind to DropDownListFor
.
using System.Collections.Generic;
namespace HelloWorld
{
public class MyViewModel
{
public int SelectedPersonID { get; set; }
public List<Person> Persons { get; set; }
}
public class Person
{
public int PersonID { get; set; }
public string First { get; set; }
public string Last { get; set; }
public string NameFull
{
get {
return First + " " + Last;
}
}
}
}
We bind the DropDownListFor
to MyViewModel
. Specifically, the first parameter is the current SelectedPersonId
, and the second parameter is a new SelectList
that we create from our view model.
@model HelloWorld.MyViewModel
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
@using (Html.BeginForm())
{
@Html.DropDownListFor(
m => m.SelectedPersonID,
new SelectList(Model.Persons, "PersonID", "NameFull"))
<button type="sumbit">Submit</button>
}
<span>Selected Person's ID is @Model.SelectedPersonID</span>
</body>
</html>
On the post back to the controller, we can retrieve the SelectedPersonID
.
using System.Web.Mvc;
using System.Collections.Generic;
using System.Linq;
namespace HelloWorld
{
public class HomeController : Controller
{
public FakeDb fakeDb = new FakeDb();
[HttpGet]
public ActionResult Index()
{
var vm = new MyViewModel {
SelectedPersonID = fakeDb.Persons.FirstOrDefault().PersonID,
Persons = fakeDb.Persons
};
return View(vm);
}
[HttpPost]
public ActionResult Index(MyViewModel vm)
{
// this is redundant
// but illustrates how to access the selected id
vm.SelectedPersonID = vm.SelectedPersonID;
vm.Persons = fakeDb.Persons;
return View(vm);
}
}
}
This is just for demo purposes.
public class FakeDb
{
public List<Person> Persons
{
get
{
var persons = new List<Person>();
for (var i = 0; i < 10; ++i)
{
persons.Add(new Person()
{
First = "First" + i,
Last = "Last" + i,
PersonID = i
});
}
return persons;
}
}
}
Upvotes: 1
Reputation: 26048
If I understand you correctly then you want the first name and last name to be displayed in the dropdown and when a user selects a name combination, then the person's ID has to be used instead? If so, then the code below is based on my assumptions above.
I have created the necessary classes as what I would have done it. Please excuse if I have left something out, the code below is not tested.
Your Person
class will look something like this:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName
{
get
{
return FirstName + " " + LastName;
}
}
}
I haven't worked with Entity Framework
in ages, not sure if it will complain about FullName
not being a column name in the persons table.
Create a PersonViewModel
class that you will pass into the view.
public class PersonViewModel
{
public PersonViewModel()
{
Persons = new List<Person>();
}
public int PersonId { get; set; }
public List<Person> Persons { get; set; }
}
In your controller you will have something like this:
public ActionResult Create()
{
PersonViewModel model = new PersonViewModel();
// Get a list of all the people
// I can't remember how to use Entity Framework to get a list of all the people,
// it's something like the code below
model.Persons = db.Persons;
return View(model);
}
Your post action method:
[HttpPost]
public ActionResult Create(PersonViewModel model)
{
// Do whatever you need to do
}
In your view the code will look like this:
@Html.DropDownListFor(
x => x.PersonId,
new SelectList(Model.Persons, "Id", "FullName", Model.PersonId),
"-- Select --"
)
The code above should display the first name and last name in the "text" part of the dropdown, and the ID of the person in the value of the dropdown.
I hope you come right :)
Upvotes: 2