从View提交清单控制器 [英] Submit a List from View to Controller

查看:145
本文介绍了从View提交清单控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以请告诉我code从视图按钮点击张贴列表MVC3-ASP到控制器。我有一个列表,其中列表中的元素之一就是检查box.at首先我必须选择一些从列表中的值,然后提交保存选择值仅。

can anyone please show me the code for posting a list on button click from view to controller in mvc3-asp. i have a list in which one of the list element is check box.at first i have to select some of the values from list then submit it to save the selected values only.

推荐答案

您的问题是两大回答。所以,我会给你你需要根据自己的情况来定制一个通用的例子。

Your question is two broad to answer. So i will give you a generic example which you need to customize according to your scenario.

假设你的观点是一些任务分配给用户。每个用户可以被分配到一个或多个任务。

Assuming your view is to assign some tasks to users. Each user can be assigned to one ore more tasks.

所以,我将创建一个视图模型是这样的。

So i will create a viewmodel like this.

public class UserViewModel 
{
  public int UserId { set;get;}
  public string Name { set;get;}
  public List<Task> Tasks { set;get;}
  public UserViewModel()
  {
     Tasks=new List<UserTask>();
  }      
}
public class UserTask
{
  public int ID { set;get;}
  public string Name { set;get;}      
  public bool IsSelected { set;get;}
}

现在在你的 GET 的行动,为您打造UserViewModel的对象和属性设置任务可用TAKS列表(从您的数据库而定)

Now in your GET action, you create an object of UserViewModel and set the Tasks property to a list of available taks (from your database may be)

public ActionResult Add()
{
  var vm=new UserViewModel();
  vm.Tasks=GetAvailableTasks();
  return View(vm);
}

假设 GetAvailableTasks 是返回 UserTask 对象列表的方法。

Assuming GetAvailableTasks is a method which returns a list of UserTask objects.

现在创建一个编辑模板。转到您的〜/查看/ YourControllerName 文件夹,并创建一个文件夹,名为 EditorTemplates 。添加一个新的视图到新创建的文件夹,并给予名称 Task.cshtml 。下面code添加到

Now create an editor templates. Go to your ~/Views/YourControllerName folder and create a folder called EditorTemplates. Add a new view to the newly created folder and give name as Task.cshtml. Add the below code to that

@model YourNameSpace.UserTask
<p>
   @Html.CheckBoxFor(x => x.IsSelected)  Model.Name
   @Html.HiddenFor(x => x.ID)
</p>

现在回到你的主视图,并使用 EditorFor helper方法带来的EditorTemplate。

Now go back to your main view and use EditorFor helper method to bring the EditorTemplate.

@model YourNamespace.UserViewModel
<h2>Quiz 24</h2>
@using (Html.BeginForm())
{
    @Html.EditorFor(x=>x.Tasks)
    <input type="submit" />
}

现在,当表单被提交,就可以得到选中的复选框值,这样

Now when the form gets submitted, you can get the selected checkbox values like this

[HttpPost]
public ActionResult Save(UserViewModel model)
{
   List<int> taskIds=new List<int>();
   foreach (var task in model.Tasks)
   {
     if (task.IsSelected)
     {
       //you can get the selected task id's here. 
       taskIds.Add(task.ID);    
     }
   } 
   //to do : save and redirect (PRG pattern)
}

这里是一篇博客文章中解释了如何使用EditorTemplates来处理表单提交的集合。后显示了一个例子来处理单选按钮。

Here is a blog post explaining how to use EditorTemplates to handle collections on form submit. the post is showing an example to handle radio buttons.

这篇关于从View提交清单控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆