我正在学习 mvc.here 我有一个模型。现在我想使用 html helper 构建表单。因此,当调用索引操作时,我想手动填充模型数据并将模型发送到查看。这是我的模型数据,但由于缺乏知识,我想要构建表单的方式是不可能的。所以如果可能的话帮助我
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。单击保存按钮时,模型数据将返回到称为保存的操作方法,例如
public ActionResult Save(Student s) { return View(s); }
或者
public ActionResult Save(StudentViewModel sv) { return View(); }
指导我如何进行。谢谢
更新
我可以用这种方式手动填充我们的模型吗
var model = new Student()
{
FirstName = "John",
LastName = "Doe",
Dob = DateTime.Now,
State = new list<State>()
{
new state({ID="1" , Name = "test1"});
new state({ID="2" , Name = "test2"});
new state({ID="3" , Name = "test3"});
};
}
特别看到这段代码....它是否有效,因为我现在不在我的电脑之前。
State = new list<State>()
{
new state({ID="1" , Name = "test1"});
new state({ID="2" , Name = "test2"});
new state({ID="3" , Name = "test3"});
};