我在使用PartialView更新ViewModel时遇到问题。 PartialView包含设备以及位置,具体取决于客户选择的DropDownList。 (在底部我提供了示例截图)。问题是,在接受提交按钮后,ViewModel(FiscalizationViewModel)中的属性设备不会更新。以下是模型的示例。我不确定我是否正在尝试妥善解决这个问题。
namespace TestMVC.ViewModels
{
public class FiscalizationViewModel
{
public int CustomerId { get; set; }
public string FiscalizationDate { get; set; }
public List<DevicesToFiscalizationViewModel> Devices { get; set; }
public FiscalizationViewModel()
{
Devices = new List<DevicesToFiscalizationViewModel>();
}
public IEnumerable<DevicesToLocalization> GetSelectedIds()
{
return (from d in Devices where d.Selected select new DevicesToLocalization() { DeviceId = d.DeviceId, LocalizationId = d.LocalizationId }).ToList();
}
}
public class DevicesToFiscalizationViewModel
{
public int DeviceId { get; set; }
public string DeviceName { get; set; }
public bool Selected { get; set; }
public string SerialNumber { get; set; }
public int LocalizationId { get; set; }
public IEnumerable<Localization> Localizations { get; set; }
public DevicesToFiscalizationViewModel()
{
Localizations = new List<Localization>();
}
}
}
以下是Customer DropDownList事件
调用的方法public PartialViewResult CustomerChanged(int CustomerId)
{
var localizations = db.Localizations.Where(i => i.CustomerId == CustomerId).ToList();
var devicesToFsc = (from d in db.Devices
select new DevicesToFiscalizationViewModel()
{
DeviceId = d.DeviceId,
DeviceName = d.Name,
SerialNumber = d.SerialNumber,
}).ToList();
foreach (var item in devicesToFsc)
{
item.Localizations = localizations;
}
return PartialView("~/Views/Fiscalizations/EditorTemplates/DevicesToFiscalizationViewModel.cshtml", devicesToFsc);
//--------------------------------
$("#customerChanged").on("change", function () {
$.ajax(
{
url: '/Fiscalizations/CustomerChanged?CustomerId=' + $(this).val(),
type: 'GET',
data: "",
contentType: 'application/json; charset=utf-8',
success: function (data) {
$("#devicesToFiscalization").html(data);
}
});
});
这只是视图(财政化创建视图)的一小部分
@model TestMVC.ViewModels.FiscalizationViewModel
<table class="table" id="devicesToFiscalization">
<thead>
...
</thead>
@Html.Partial("~/Views/Fiscalizations/EditorTemplates/DevicesToFiscalizationViewModel.cshtml", Model.Devices)
</table>
PartialView:
@model IEnumerable<TestMVC.ViewModels.DevicesToFiscalizationViewModel>
@foreach(var item in Model)
{
<tbody>
<tr>
<td style="text-align:center">
<div class="checkbox">
@Html.EditorFor(m => item.Selected)
</div>
</td>
<td>
@Html.DisplayFor(m => item.DeviceName)
</td>
<td>
@Html.DisplayFor(m => item.SerialNumber)
</td>
<td>
@Html.DropDownList("LocalizationId", new SelectList(item.Localizations, "LocalizationId", "Name"), "Select", htmlAttributes: new { @class = "form-control", style = "width: 200px;" })
</td>
<td>
@Html.HiddenFor(m => item.DeviceId)
</td>
</tr>
</tbody>
答案 0 :(得分:0)
根据我对您的观点的不足,您的问题是针对主视图和部分视图使用不同的模型。您应该在模型绑定中使用完全相同的模型,可以在服务器端更新模型。保留两个模型TestMVC.ViewModels.FiscalizationViewModel
或IEnumerable<TestMVC.ViewModels.DevicesToFiscalizationViewModel>
答案 1 :(得分:0)
它有效:)下面我展示它如何看待变化
主要观点:
@model TestMVC.ViewModels.FiscalizationViewModel
<div id="devicesToFiscalization">
@Html.Partial("~/Views/Fiscalizations/EditorTemplates/DevicesToFiscalizationViewModel.cshtml", Model)
</div>
部分观点:
@model TestMVC.ViewModels.FiscalizationViewModel
<table class="table">
<thead>
<tr>
<th>
Select
</th>
<th>
Name
</th>
<th>
Serial number
</th>
<th>
Localization
</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Devices.Count; i++)
{
<tr>
<td style="text-align:center">
<div class="checkbox">
@Html.EditorFor(m => m.Devices[i].Selected)
</div>
</td>
<td>
@Html.DisplayFor(m => m.Devices[i].DeviceName)
</td>
<td>
@Html.DisplayFor(m => m.Devices[i].SerialNumber)
</td>
<td>
@Html.DropDownListFor(m => m.Devices[i].LocalizationId, new SelectList(Model.Devices[i].Localizations, "LocalizationId", "Name"), "Select", htmlAttributes: new { @class = "form-control", style = "width: 200px;" })
</td>
<td>
@Html.HiddenFor(m => m.Devices[i].DeviceId)
</td>
</tr>
}
</tbody>
和CustomerChanged方法:
public PartialViewResult CustomerChanged(int CustomerId)
{
var localizations = db.Localizations.Where(i => i.CustomerId == CustomerId).ToList();
var Devices = (from d in db.Devices
select new DevicesToFiscalizationViewModel()
{
DeviceId = d.DeviceId,
DeviceName = d.Name,
SerialNumber = d.SerialNumber,
}).ToList();
foreach (var item in Devices)
{
item.Localizations = localizations;
}
var fsc = new FiscalizationViewModel();
fsc.Devices = Devices;
return PartialView("~/Views/Fiscalizations/EditorTemplates/DevicesToFiscalizationViewModel.cshtml", fsc);
}
=============================================== =========================
我正在尝试使用EditorFor编写此内容但是我在正确编写 CustomerChanged 方法时遇到问题,该方法返回设备列表并且不期望此EditorTemplate看起来如下所示:
@model TestMVC.ViewModels.DevicesToFiscalizationViewModel
<tr>
<td style="text-align:center">
<div class="checkbox">
@Html.EditorFor(m => m.Selected)
</div>
</td>
<td>
@Html.DisplayFor(m => m.DeviceName)
</td>
<td>
@Html.DisplayFor(m => m.SerialNumber)
</td>
<td>
@Html.DropDownListFor(m => m.LocalizationId, new SelectList(Model.Localizations, "LocalizationId", "Name"), "Select", htmlAttributes: new { @class = "form-control", style = "width: 200px;" })
</td>
<td>
@Html.HiddenFor(m => m.DeviceId)
</td>
</tr>
主要观点:
<table class="table" id="devicesToFiscalization">
...
<tbody>
@Html.EditorFor(m => m.Devices)
</tbody>
</table>