C# 回发时,asp net MVC3对象提交列表为空

C# 回发时,asp net MVC3对象提交列表为空,c#,asp.net-mvc-3,list,null,C#,Asp.net Mvc 3,List,Null,我使用asp net MVC 3作为我的一个项目。我使用部分视图进行编码。我想在列表中列出所有客户,并将他们的信息作为列表提交。当我试图在post back中提交我的列表时,它发送的列表为空。您可以在下面找到我的代码: 我的控制器方法是: [HttpPost] public ActionResult ConfirmUsers(ICollection<Career.DomainModel.UserApprovalDto> collection) { st

我使用asp net MVC 3作为我的一个项目。我使用部分视图进行编码。我想在列表中列出所有客户,并将他们的信息作为列表提交。当我试图在post back中提交我的列表时,它发送的列表为空。您可以在下面找到我的代码:

我的控制器方法是:

[HttpPost]
    public ActionResult ConfirmUsers(ICollection<Career.DomainModel.UserApprovalDto> collection)
    {
        string bas = "";
        //if (collection != null)

        if (ModelState.IsValid)
        {
            bas = "bas";
        }

        return RedirectToAction("Index");
    }
[HttpPost]
公共操作结果确认用户(ICollection集合)
{
字符串bas=“”;
//if(集合!=null)
if(ModelState.IsValid)
{
bas=“bas”;
}
返回操作(“索引”);
}
我的部分看法是:

@model List<Career.DomainModel.UserApprovalDto>
@using (Html.BeginForm("ConfirmUsers", "ManageUsers", new { area = "" }, FormMethod.Post))
{
    <table>
        <tr>
            <th>
                Name
            </th>
            <th>
                Is Reported
            </th>
        </tr>
        @for (int i = 0; i < Model.Count(); i++)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => Model[i].FirstName)
                </td>
                <td>
                    @Html.CheckBox("IsReported", Model[i].IsReported.HasValue ? Model[i].IsReported.Value : false)
                    @*@Html.CheckBoxFor(modelItem => Model[i].IsReported.Value);*@ @*    @if (Model[i].IsReported != null)
                    {
                        @Html.CheckBoxFor(modelItem => Model[i].IsReported.Value);
                    }
                    else
                    {
                        @Html.CheckBoxFor(modelItem => Model[i].IsReported.Value);
                    }*@
                </td>
                <td>
                </td>
            </tr>
        }
    </table>
    <div>
        <input name="submitUsers" type="submit" value="Save" />
    </div>
}
@型号列表
@使用(Html.BeginForm(“ConfirmUsers”、“ManageUsers”、新的{area=”“}、FormMethod.Post))
{
名称
据报道
@对于(int i=0;iModel[i].FirstName)
@复选框(“IsReported”,模型[i]。IsReported.HasValue?模型[i]。IsReported.Value:false)
@*@Html.CheckBoxFor(modelItem=>Model[i].IsReported.Value);*@*@if(Model[i].IsReported!=null)
{
@CheckBoxFor(modeleItem=>Model[i].IsReported.Value);
}
其他的
{
@CheckBoxFor(modeleItem=>Model[i].IsReported.Value);
}*@
}
}
提前谢谢


Kerem

我会使用编辑器模板来处理这个问题。使用这样的视图模型来表示复选框项

public class ReportedUserViewModel 
{
  public string FirstName { set;get;}
  public int Id { set;get;}
  public bool IsSelected { set;get;}
}
现在在主视图模型中,添加一个属性,该属性是上述类的集合

public class ConfirmUserViewModel
{      
  public List<ReportedUserViewModel> ReportedUsers{ get; set; }      
  //Other Properties also here

  public ConfirmUserViewModel()
  {
    ReportedUsers=new List<ReportedUserViewModel>();       
  }    
}
现在让我们创建一个EditorTemplate。转到
Views/YourControllerName
并创建一个名为EditorTemplate的文件夹,在那里创建一个与属性名同名的新视图(
ReportedUsers.cshtml

将此代码添加到新创建的编辑器模板中

@model ReportedUserViewModel 
<p>
  <b>@Model.FirstName </b>   :
  @Html.CheckBoxFor(x => x.IsSelected) <br />
  @Html.HiddenFor(x=>x.Id)
</p>
现在,当您发布表单时,您的模型将具有
ReportedUsers
集合,其中选中的复选框将为
IsSelected
属性具有真实值

[HttpPost]
public ActionResult AddAlert(ConfirmUserViewModel model)
{
   if(ModelState.IsValid)
   {
      //Check for model.ReportedUsers collection and Each items
      //  IsSelected property value.
      //Save and Redirect(PRG pattern)
   }
   return View(model);
}

对于您编写的代码,MVC模型绑定器机制不知道如何将这些输入映射到对象列表中

做这个小把戏:

@Html.CheckBox(“[”+i+“]。“+”IsReported”,Model[i].IsReported.Value)

这将导致输入字段的名称为[0]。为列表中的第一项报告[1]。为下一项报告

这应该行得通


我参加聚会有点晚了,但是如果将列表嵌入模型中,只需参考列表就很容易了--

这将为迭代器中的每个项发回。

@Html.CheckBox(“[”+i+“]。“+”IsReported”,Model[i].IsReported.Value); 对我来说非常有效

确保post方法包含列表的参数。 e、 g公共操作结果索引(ConfigViewModel模型、列表配置列表)

在本例中,我有一个视图模型(model),其中包含列表对象。
如果您在post方法中按原样指定视图模型,那么您将获得列表的null值(此处模型对象为null)。但是,如果在操作中添加特定的列表参数(configurationList),则可以在控制器中获取所有列表值。

我上周遇到了相同的问题

当我从数据库中获取复选框时,我意识到它有三个值(true/false/null),因为我在设计数据库时允许复选框值为null。我重新设计了db,问题就解决了


你没有发布模型,所以我不确定是否是这样。只要看看这个模型,如果它在您的Ischeck属性上面写的是Nullable,请转到数据库并重新设计。记住isCheck,isSelected属性必须只有两个值(true/false)。

hi Shyju,我使用了编辑器模板。但是,我只有一个模型作为DTO。所以,我使用列表作为我的数据。谢谢你的回复。
@model ConfirmUserViewModel
@using (Html.BeginForm())
{
    <div>  
      @Html.EditorFor(m=>m.ReportedUsers)         
    </div>    
    <input type="submit" value="Submit" />
}
[HttpPost]
public ActionResult AddAlert(ConfirmUserViewModel model)
{
   if(ModelState.IsValid)
   {
      //Check for model.ReportedUsers collection and Each items
      //  IsSelected property value.
      //Save and Redirect(PRG pattern)
   }
   return View(model);
}
@Html.CheckBoxFor(modelItem => Model.List[i].Selected)