gpt4 book ai didi

c# - asp net MVC3 回发时提交对象列表为空

转载 作者:太空宇宙 更新时间:2023-11-03 13:52:24 26 4
gpt4 key购买 nike

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

我的 Controller 方法是:

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

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

return RedirectToAction("Index");
}

我的部分观点是:

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

提前致谢。

凯伦姆

最佳答案

我会使用编辑器模板 来处理这个问题。让您的 View 模型像这样表示 CheckBox 项。

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>();
}
}

现在在您的 GET 操作中,您将填充 ViewModel 的值并将其发送到 View 。

public ActionResult ConfirmUser()
{
var vm = new ConfirmUserViewModel();

//The below code is hardcoded for demo. you mat replace with DB data.
vm.ReportedUsers.Add(new ReportedUserViewModel { Name = "Test1" , Id=1});
vm.ReportedUsers.Add(new ReportedUserViewModel { Name = "Test2", Id=2 });

return View(vm);
}

现在让我们创建一个 EditorTemplate。转到 Views/YourControllerName 并创建一个名为 EditorTemplate 的文件夹,并在那里创建一个与属性名称同名的新 View (ReportedUsers.cshtml)

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

@model ReportedUserViewModel 
<p>
<b>@Model.FirstName </b> :
@Html.CheckBoxFor(x => x.IsSelected) <br />
@Html.HiddenFor(x=>x.Id)
</p>

现在在您的主视图中,使用 EditorFor Html Helper 方法调用您的编辑器模板。

@model ConfirmUserViewModel
@using (Html.BeginForm())
{
<div>
@Html.EditorFor(m=>m.ReportedUsers)
</div>
<input type="submit" value="Submit" />
}

现在,当您发布表单时,您的模型将拥有 ReportedUsers 集合,其中选中的复选框将为 IsSelected 属性提供 True 值。

[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);
}

关于c# - asp net MVC3 回发时提交对象列表为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13327861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com