我有一个网格,每行包含一个复选框。如果选中该复选框并且用户点击“提交”按钮,我希望它将数据库中的“已确认”字段更新为“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);
}
}
}
答案 0 :(得分:1)
有点不清楚为什么你的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" ...>
将正确绑定到您的收藏。
答案 1 :(得分:0)
你真的需要重新思考你是如何做到这一点的。首先,复选框需要一个值,否则你怎么知道选择哪一个!?接下来,我将创建两个html表单,一个用于页面顶部的数据,一个用于列表/表。然后在控制器中创建一个新动作,它将整数列表作为一个与您的复选框ID相同的参数,将第二个表单发布到此操作,查找列表中带有ID的所有记录,并将字段设置为Y.简单!