简体   繁体   English

如何将非模型字段发送回动作表单视图

[英]How to send non model fields back to action form view

I have a view that has a model with a list of items I build as check box values. 我有一个视图,该视图具有一个模型,其中包含作为复选框值构建的项目列表。 How can I post those values back to the action with the model? 如何将这些值发布回模型的操作? The problem is that there is not a concrete amount f check boxes, and they are sometimes unknown what the value will be. 问题是没有具体的数量f复选框,有时它们的值是未知的。 There could be 2 or there could be 15 depending on the user. 取决于用户,可能有2个或有15个。 but are built from the model's list values. 但是是根据模型的列表值构建的。 Thanks in advance 提前致谢

You can use editor templates . 您可以使用编辑器模板

Assume, your view/page is to assign courses to students. 假设您的视图/页面是为学生分配课程。 So you will have a viewmodel like this 所以你将有一个这样的viewmodel

public class AssignCourseVM
{
  public int StudentID { set;get;}
  public string StudentName { set;get;}
  public List<CourseRegistration> Courses { set;get;}
  public AssignCourseVM()
  {
    Courses =new List<CourseRegistration>();
  }
}
public class CourseRegistration
{
  public int CourseID { set;get;}
  public string CourseName { set;get;}
  public bool IsRegistered { set;get;}
}

Now in your GET action, You will create an object of your viewmodel and send to the view 现在,在GET操作中,您将创建视图模型的对象并将其发送到视图

public ActionResult Registration()
{
    var vm = new AssignCourseVM();

    //Student Info is hard coded. You may get it from db
    vm.StudentID = 1;
    vm.StudentName = "Scott";
    vm.Courses = GetCourseRegistations();
    return View(vm);
}
public List<CourseRegistration> GetCourseRegistations()
{
    var list = new List<CourseRegistration>();
    //Hard coded for demo. You may load this list from DB
    list.Add(new CourseRegistration { CourseID = 1, CourseName = "EN" });
    list.Add(new CourseRegistration { CourseID = 2, CourseName = "GE" });
    return list;
}

Now Let's create an Editor Template, Go to The View/YourControllerName and Create a Folder called EditorTemplates and Create a new View there with the same name as of the Property type ( CourseRegistration.cshtml ). 现在,让我们创建一个编辑器模板,转到View/YourControllerName并创建一个名为EditorTemplates的文件夹,并在其中创建一个与属性类型( CourseRegistration.cshtml )同名的新视图。

在此处输入图片说明

Now inside this new file, paste this content 现在,在此新文件中,粘贴此内容

@model ReplaceYourProjectNameSpaceHere.ViewModels.CourseRegistration
<div>
    @Model.CourseName : @Html.CheckBoxFor(s=>s.IsRegistered)
    @Html.HiddenFor(s => s.CourseID)
</div>

Now in our main view ( Registration.cshtml ) which is strongly typed to our AssignCourseVM class, we will use Html.EditorFor helper method. 现在,在强类型AssignCourseVM类的主视图( Registration.cshtml )中,我们将使用Html.EditorFor helper方法。

@model ReplaceYourProjectNameSpaceHere.ViewModels.AssignCourseVM
<h2>Registration</h2>
@using(Html.BeginForm())
{
    <h4>@Model.StudentName</h4>
    @Html.EditorFor(s=>s.Courses)
    @Html.HiddenFor(s=>s.StudentID)
    <input type="submit" />
}

Now when user posts the form, You can inspect the posted viewmodel's course property to see which items are checked or not. 现在,当用户发布表单时,您可以检查发布的viewmodel的course属性,以查看是否检查了哪些项目。

[HttpPost]
public ActionResult Registration(AssignCourseVM model)
{
    //to do :save and redirect
    return RedirectToAction("RegistrationSuccessfull");
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM