开发者

MVC Model Binding and DropDown List

开发者 https://www.devze.com 2023-01-11 07:21 出处:网络
I have a Person class displayed below.In the view I need to display its properties, plus the DepartmentName that exists in a different Table.DepartmentId in the class below is a Foreign Key.I am using

I have a Person class displayed below. In the view I need to display its properties, plus the DepartmentName that exists in a different Table. DepartmentId in the class below is a Foreign Key. I am using Linq to SQL. I am also display my PersonRepository and Controller code below. I need to display the DepartmentName in the Index View page and a dropdown list on the Edit View Page. I am new to MVC, so I am not sure how to do this. Any help is very much appreciated.

public class Person_Validation
{
    [HiddenInput(DisplayValue = false)]
    [ScaffoldColumn(false)]
    public object PersonId { get; set; }

    [HiddenInput(DisplayValue = false)]
    [ScaffoldColumn(false)]
    public object DepartmentId { get; set; }

 开发者_StackOverflow   [DisplayName("First Name")]
    [Required(ErrorMessage = "First Name is required")]
    [StringLength(50, ErrorMessage = "Frist Name cannot be more than 50 characters")]
    public object FirstName { get; set; }

    [DisplayName("Last Name")]
    [Required(ErrorMessage = "Last Name is required")]
    [StringLength(50, ErrorMessage = "Last Name cannot be more than 50 characters")]
    public object LastName { get; set; }

    [HiddenInput(DisplayValue = false)]        
    public object Active { get; set; }

    [HiddenInput(DisplayValue = false)]
    public object DateAdded { get; set; }

    [HiddenInput(DisplayValue = false)]
    public object DateDeleted { get; set; }

    public IEnumerable<Department> departments { get; set; }             
}

PersonRepository class:

public IList<Person> GetAllPersons()
{
    //get all active people
    var activePeople = from p in db.Persons
                       where p.Active == true
                       select p;

    return activePeople.ToList();            
}

Controller code:

public ActionResult Index(int? page)
{
    const int pageSize = 25;

    var persons = _repository.GetAllPersons();            
    var paginatedPersons = new PaginatedList<Person>(persons, page ?? 0, pageSize);

    return View(paginatedPersons);
}


I recommend you to look at the asp.net mvc sample from here http://valueinjecter.codeplex.com, especially at the TinyController which I think is the best way of doing mvc


I would like to share the link, where you can get various ways to bind the dropdown list using MVC

http://www.c-sharpcorner.com/UploadFile/deveshomar/ways-to-bind-dropdown-list-in-Asp-Net-mvc/

It might be helpful to you.

0

精彩评论

暂无评论...
验证码 换一张
取 消