Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 MVC中如何将表单与模型数据绑定_Asp.net Mvc 3_Model_Asp.net Mvc Viewmodel - Fatal编程技术网

Asp.net mvc 3 MVC中如何将表单与模型数据绑定

Asp.net mvc 3 MVC中如何将表单与模型数据绑定,asp.net-mvc-3,model,asp.net-mvc-viewmodel,Asp.net Mvc 3,Model,Asp.net Mvc Viewmodel,这里我有一个模型。现在我想使用html帮助器构建表单。因此,当调用索引操作时,我希望手动填充模型数据并将模型发送到视图。 这是我的模型数据,但由于缺乏知识,我想要构建表单的方式不可能实现。所以如果可能的话,请帮助我 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using System

这里我有一个模型。现在我想使用html帮助器构建表单。因此,当调用索引操作时,我希望手动填充模型数据并将模型发送到视图。 这是我的模型数据,但由于缺乏知识,我想要构建表单的方式不可能实现。所以如果可能的话,请帮助我

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

请帮助新手学习。谢谢

这是一篇关于各种类型的模型绑定的好文章。它不会太长,并且消除了自动绑定和手动方式的神秘性

我也同意这个问题的范围有点太广。我会尽力帮忙的。对于get和post请求,您可以使用相同的方法名(加载空页面的方法名是get,当您填写表单时,您将表单数据“post”回post方法)。您需要指定哪一个使用http“get”,哪一个使用“post”,您可以使用这样的属性来装饰您的方法

[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
}

这是学校的作业吗?或者你可以停止懒惰,学会自己做。这不是一个要求人们免费为你做所有工作的网站。
[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
}