简体   繁体   中英

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. There could be 2 or there could be 15 depending on the user. 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

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

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 ).

在此处输入图片说明

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.

@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.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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