【问题标题】:How to bind form with model data in MVC如何在 MVC 中将表单与模型数据绑定
【发布时间】:2013-09-16 20:40:30
【问题描述】:

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

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) 对于我想显示文本框的名字和姓氏属性

2) 对于我想用日期选择器显示文本框的 DOB 属性

3) 对于我想用日期选择器显示文本框的 DOB 属性

4) 对于我想显示下拉列表或组合的州/城市属性

5) 对于我想要显示复选框组的 Language 属性

6) 对于 Sex 属性,我想为男性和女性显示 2 个单选按钮

7) 用于我要显示列表框的计算机课程属性

现在编写一个索引操作方法,该方法将使用虚拟数据填充模型并生成 UI。 当点击保存按钮时,模型数据将返回到名为 save like 的操作方法

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

or 

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

请帮助新手学习东西。谢谢

【问题讨论】:

  • 这是学校的作业?
  • 或者你可以停止偷懒,学会自己做。这不是一个要求人们免费为您完成所有工作的网站。

标签: asp.net-mvc-3 model asp.net-mvc-viewmodel


【解决方案1】:

这是一篇关于各种类型的模型绑定的好文章。它不会太长,并且揭开了自动绑定和手动绑定的神秘面纱。

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

我也同意这个问题的范围有点太宽泛了。我会尽力提供一点帮助。您可以为 get 和 post 请求使用相同的方法名称(加载空白页面的是 get,当您填写表单时,您会将表单数据“发布”回 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
}

【讨论】:

    猜你喜欢
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 2016-08-24
    • 1970-01-01
    相关资源
    最近更新 更多