我有一个网格,每行都包含一个复选框。如果选中复选框,并且用户点击"提交"按钮,我希望它将数据库中的"已确认"字段更新为"Y"。我遇到的问题是,在发布过程中,记录数据没有传递给控制器。
"TransactionList"为null。其他属性工作正常。TransactionList是存储所有记录数据的地方(名称、mtf等)
包含交易列表的我的视图模型
namespace CCQAS.WebApp.Areas.Credentialing.Models
{
public class CredTransactionsListViewModel
{
public List<CredTransactionsViewModel> TransactionList { get; set; }
public string Status { get; set; }
public string Action { get; set; }
public bool Incoming { get; set; }
public bool Outgoing { get; set; }
public bool Primary { get; set; }
public bool ReadOnly { get; set; }
public bool CanInsert { get; set; }
public bool CanDelete { get; set; }
public bool Search { get; set; }
}
}
我的视图
@model CCQAS.WebApp.Areas.Credentialing.Models.CredTransactionsListViewModel
@using CCQAS.WebApp.Areas.Credentialing.Models
@using CCQAS.API.Model
@{
ViewBag.Title = "Transactions";
}
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">
@Html.Partial("_SectionHelp", (string)ViewBag.HelpText)
@ViewBag.Title
</h3>
</div>
@using (Html.BeginForm("UpdateAcknowledgement", "CredTransactions", FormMethod.Post, new { @id = "cred-transactions", @role = "form" }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.Search)
<div class="col-sm-3 col-md-3">
<fieldset>
<th><b>Direction</b></th>
<div class="checkbox">
<label>
@Html.CheckBoxFor(model => model.Incoming) Incoming
</label>
</div>
<div class="checkbox">
<label>
@Html.CheckBoxFor(model => model.Outgoing) Outgoing
</label>
</div>
<div class="checkbox">
<label>
@Html.CheckBoxFor(model => model.Primary) Primary
</label>
</div>
</fieldset>
</div>
<div class="col-sm-3 col-md-3">
<fieldset>
<th><b>Status</b></th>
<div class="radio">
<label>
@Html.RadioButtonFor(model => Model.Status, "UNACKNOWLEDGED") Unacknowledged
</label>
</div>
<div class="radio">
<label>
@Html.RadioButtonFor(model => model.Status, "ACKNOWLEDGED") Acknowledged
</label>
</div>
<div class="radio">
<label>
@Html.RadioButtonFor(model => model.Status, "") Both
</label>
</div>
</fieldset>
</div>
<div class="col-sm-3 col-md-3">
<fieldset>
<th><b>Action</b></th>
<div class="radio">
<label>
@Html.RadioButtonFor(model => Model.Action, "PCS") PCS
</label>
</div>
<div class="radio">
<label>
@Html.RadioButtonFor(model => model.Action, "ICTB") ICTB
</label>
</div>
<div class="radio">
<label>
@Html.RadioButtonFor(model => model.Action, "") All
</label>
</div>
</fieldset>
</div>
<div class="col-sm-3 col-md-3">
<div></div>
<div class="radio">
<label>
@Html.RadioButtonFor(model => Model.Action, "DOCUMENTS") Update of Credentials Requested
</label>
</div>
<div class="radio">
<label>
@Html.RadioButtonFor(model => model.Action, "NONPRIMARYASSIGNMENT") Non-Primary Assignment Created
</label>
</div>
<div class="radio">
<label>
@Html.RadioButtonFor(model => model.Action, "CUSTODY") Custody Transfer
</label>
</div>
</div>
<table class="table table-striped table-bordered data-table" id="myTable" name="myTable">
<thead>
<tr>
<th class="hidden"></th>
<th class="text-center">
Acknowledged
</th>
<th class="text-center">
From MTF
</th>
<th class="text-center">
To MTF
</th>
<th class="text-center">
Primary MTF
</th>
<th class="text-center">
Action
</th>
<th class="text-center">
Initiated
</th>
<th class="text-center">
Provider Name
</th>
<th class="text-center">
SSN
</th>
<th class="text-center">
Sender's Name
</th>
<th class="text-center">
Sender's Phone
</th>
</tr>
</thead>
@foreach (CredTransactionsViewModel credTransactionsViewModel in Model.TransactionList)
{
long credProviderId = credTransactionsViewModel.CredProviderId;
<tr>
<td class="hidden"> @credTransactionsViewModel.ProviderTransactionId</td>
<td class="text-center">
@if (credTransactionsViewModel.AcknowledgedChar == "Y")
{
<input type="checkbox" name=@credTransactionsViewModel.Acknowledged id="chkAcknowledged" checked="checked" />
}
else
{
<input type="checkbox" name=@credTransactionsViewModel.Acknowledged id="chkAcknowledged" />
}
</td>
<td>
@credTransactionsViewModel.FromUicTxt
</td>
<td>
@credTransactionsViewModel.ToUicTxt
</td>
<td>
@credTransactionsViewModel.PrimaryUicTxt
</td>
<td>
@credTransactionsViewModel.Action
</td>
<td>
@credTransactionsViewModel.Initiated
</td>
<td>
@credTransactionsViewModel.ProviderFirst @credTransactionsViewModel.ProviderLast
</td>
<td>
@credTransactionsViewModel.SSN
</td>
<td>
@credTransactionsViewModel.SendersFirst @credTransactionsViewModel.SendersLast
</td>
<td>
@credTransactionsViewModel.SendersPhone
</td>
</tr>
}
</table>
<div class="panel-footer text-center">
<button type="submit" data-loading-text="Searching..." class="btn btn-primary" autocomplete="off">Search</button>
</div>
}
我的控制器
using CCQAS.API.Abstractions.Service;
using CCQAS.API.Model;
using CCQAS.API.Model.Enums.FunctionalAreas;
using CCQAS.API.Model.Security.Enum;
using CCQAS.API.Web.UI;
using CCQAS.WebApp.Areas.Credentialing.Models;
using CCQAS.WebApp.Web.Mvc;
using CCQAS.WebApp.Web.Mvc.ActionFilters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace CCQAS.WebApp.Areas.Credentialing.Controllers
{
public class CredTransactionsController : CredNavBaseController
{
#region Private
private ILookupService lookUpService;
private ICredTransactionsService credTransactionsService;
private IMtfService mtfService;
#endregion Private
#region Constructors
/// <summary>
/// Controller for Cred Transactions
/// </summary>
/// <param name="credTransactionsService"></param>
public CredTransactionsController(ILookupService lookUpService, ICredTransactionsService credTransactionsService, IMtfService mtfService)
{
this.lookUpService = lookUpService;
this.credTransactionsService = credTransactionsService;
this.mtfService = mtfService;
}
#endregion Constructors
// GET: Credentialing/CredTransactions
public ActionResult GetTransactionList(CredTransactionsListViewModel model)
{
string status = string.Empty;
string action = string.Empty;
string incomingFilter = string.Empty;
string outgoingFilter = string.Empty;
string primaryFilter = string.Empty;
long mtfId = (long)this.CurrentUser.CurrentMtf.MtfId;
if (ModelState.IsValid)
{
Permission permission = this.CurrentUser.GetPermission(ProviderArea.Transaction);
//user mtf id
//var model = new CredTransactionsListViewModel();
if (model.Search != true)
{
status = "UNACKNOWLEDGED";
action = "";
incomingFilter = "Y";
outgoingFilter = "";
primaryFilter = "";
model.Status = "UNACKNOWLEDGED";
model.Action = "";
model.Incoming = true;
model.Outgoing = false;
model.Primary = false;
}
else
{
status = model.Status;
action = model.Action;
if (model.Incoming == true)
{
incomingFilter = "Y";
}
if (model.Outgoing == true)
{
outgoingFilter = "Y";
}
if (model.Primary == true)
{
primaryFilter = "";
}
}
var credTransactionList = this.credTransactionsService.GetTransactionList(mtfId, status, action, incomingFilter, outgoingFilter, primaryFilter );
model.TransactionList = (from credTransactions in credTransactionList
select new CredTransactionsViewModel
{
ProviderTransactionId = credTransactions.ProviderTransactionId,
CredAssignmentId = credTransactions.CredAssignmentId,
CredProviderId = credTransactions.CredProviderId,
CredCustodyId = credTransactions.CredCustodyId,
ProviderFirst = credTransactions.ProviderFirst,
ProviderLast = credTransactions.ProviderLast,
ProviderName = credTransactions.ProviderName,
SSN = credTransactions.SSN,
FromMTF =credTransactions.FromMTF,
ToMTF = credTransactions.ToMTF,
PrimaryMTF = credTransactions.PrimaryMTF,
FromUicTxt = credTransactions.FromUicTxt,
ToUicTxt = credTransactions.ToUicTxt,
PrimaryUicTxt = credTransactions.PrimaryUicTxt,
Action = credTransactions.Action,
AcknowledgedChar = credTransactions.AcknowledgedChar,
Acknowledged = credTransactions.Acknowledged,
AcknowledgedDate = credTransactions.AcknowledgedDate,
AcknowledgedUserId = credTransactions.AcknowledgedUserId,
Initiated = credTransactions.Initiated,
SendersName = credTransactions.SendersName,
SendersFirst = credTransactions.SendersFirst,
SendersLast = credTransactions.SendersLast,
SendersPhone = credTransactions.SendersPhone,
InitiatedUserId = credTransactions.InitiatedUserId,
ProviderTransactionTypeId = credTransactions.ProviderTransactionTypeId
}
).ToList();
//var ReadOnly = this.IsCredReadOnly(permission);
SetPageMessage(API.Web.UI.PageMessageType.Information, "Results showing last 6 months of history");
return View(model);
}
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UpdateAcknowledgement(CredTransactionsListViewModel model)
{
long userId = this.CurrentUser.UserId;
long mtfId = (long)this.CurrentUser.CurrentMtf.MtfId;
foreach (CredTransactionsViewModel credTransactionsViewModel in model.TransactionList.Where(m => m.Acknowledged ==true))
{
long providerTransactionId = credTransactionsViewModel.ProviderTransactionId;
providerTransactionId = this.credTransactionsService.UpdateTransaction(providerTransactionId, userId, mtfId);
}
return View("GetTransactionList", model);
}
}
}
有点不清楚为什么您的CredTransactionsViewModel
具有属性List<CredTransactionsViewModel>
,而您的模型似乎不包含名为Acknowledged
的属性,所以我怀疑视图模型的设计存在一些重大问题,但TransactionList
为空的原因是您没有生成任何与属性TransactionList
相关的控件。您的foreach
循环只是在没有索引器的情况下生成重复的ID
属性(无效html)和重复的name
属性。您需要使用for
循环(或自定义EditorTemplate
)。
假设ProviderTransactionId
和bool Acknowledged
确实存在,那么它将是
for (int i = 0; i < model.TransactionList.Count; i++)
{
@Html.HiddenFor(m => m.TransactionList[i].ProviderTransactionId)
@Html.CheckBoxFor(m => m.TransactionList[i].Acknowledged)
@Html.DisplayFor(m => m.TransactionList[i].FromUicTxt)
....
}
注意,for
循环生成的html是
<input name="TransactionList[0].ProviderTransactionId" ...>
<input name="TransactionList[1].ProviderTransactionId" ...>
它将正确绑定到您的收藏。
您真的需要重新思考如何做到这一点。首先,复选框需要一个值,否则你怎么知道选择了哪个!?接下来,我将创建两个html表单,一个用于页面顶部的数据,另一个用于列表/表。然后在控制器中创建一个新的操作,该操作将整数列表作为与复选框id相同的参数,将第二个表单发布到该操作中,查找列表中id为的所有记录,并将字段设置为Y。简单!