Frage

here i have one model. now i want to buil form using html helper . so when index action will be called then i want to populate model data by hand and send the model to view. here is my model data but the way i want to build form not being possible just due to lack of knowledge. so if possible help me

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;

namespace MvcPractise.Models
{
    public class Student
    {
        [Required(ErrorMessage = "First Name Required")] // textboxes will show
        [Display(Name = "First Name :")]
        [StringLength(5, ErrorMessage = "First Name cannot be longer than 5 characters.")]
        public string FirstName { get; set; }

        [Required(ErrorMessage = "Last Name Required")] // textboxes will show
        [Display(Name = "Last Name :")]
        [StringLength(5, ErrorMessage = "Last Name cannot be longer than 5 characters.")]
        public string LastName { get; set; }

        [Required(ErrorMessage = "DOB require")] // datepicker will show
        [Display(Name = "DOB :")]
        [DataType(DataType.Date)]
        public DateTime Dob { get; set; }

        [Required(ErrorMessage = "State Required")] // drodown will show
        [Display(Name = "State :")]
        public List<State> State { get; set; }

        [Required(ErrorMessage = "City Required")] // drodown will show
        [Display(Name = "City :")]
        public List<City> City { get; set; }

        [Required(ErrorMessage = "Language known Required")] // group of checkboxes will show
        [Display(Name = "Language known :")]
        public List<Language> Language { get; set; }

        [Required(ErrorMessage = "Sex Required")] // group of radio button will show
        [Display(Name = "Sex :")]
        public List<Sex> Sex { get; set; }

        [Required(ErrorMessage = "Computer Course Required")] // listbox will show
        [Display(Name = "Computer Course Done :")]
        public List<ComputerCourse> ComputerCourse { get; set; }

    }

    public class State
    {
        public string ID { get; set; }
        public string Name { get; set; }
    }

    public class City
    {
        public string ID { get; set; }
        public string Name { get; set; }
    }

    public class Language
    {
        public string ID { get; set; }
        public string Name { get; set; }
    }

    public class Sex
    {
        public string ID { get; set; }
        public string Type { get; set; }
    }

    public class ComputerCourse
    {
        public string ID { get; set; }
        public string Type { get; set; }
    }
}

1) for the first name & last name property i want to show textboxes

2) for the DOB property i want to show textbox with date picker

3) for the DOB property i want to show textbox with date picker

4) for the State/City property i want to show dropdown or combo

5) for the Language property i want to show group of checkboxes

6) for the Sex property i want to show 2 radio button for male & female

7) for the Computer Course property i want to show listbox

now write a index action method which will populate model with dummy data and generate UI. when click on save button then model data will return back to action method called saved like

public ActionResult Save(Student s)
{
   return View(s);
}

or 

public ActionResult Save(StudentViewModel sv)
{
   return View();
}

please help a newbie to learn things. thanks

War es hilfreich?

Lösung

Here is a good article about various types of model binding. It's not too long and demystifies the automatic binding and the manual way of doing it.

http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx

Also I would agree that this Question is a little too broad in scope. I'll try to help a little. You can use the same method name for the get and post request (The one that loads the empty page is the get, and when you fill out the form you will "post" the form data back to the post method). You will want to specify which one is using http 'get' and which one is using 'post' and you can do so with by decorating your method with an attribute like this.

[HttpGet]
public ActionResult Create()//leave it empty youre about to make it
{
     var model = new ObjectType();
     return view(model); // pass the new model ( empty object ) to your view;
}

[HttpPost]
public ActionResult Create(ObjectType theObject){
//MVC will try to populate the values of properties 
//of theObject if they match the names on your form elements
//save it in your database
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top