gpt4 book ai didi

c# - ViewModel 在操作方法中获取空值

转载 作者:太空狗 更新时间:2023-10-29 23:06:36 25 4
gpt4 key购买 nike

我正在使用 ViewModel 来检索在 Controller 操作中输入的数据。但是 ViewModel 在其属性中获取空值。我正在创建一个局部 View

在那个部分 View 中,我通过绑定(bind) ViewModel 创建下拉列表,然后我在其他 View 呈现那个部分 View >

下面是我的代码

我的 View 模型:

public class LookUpViewModel
{
RosterManagementEntities rosterManagementContext = new RosterManagementEntities();
public LookUpViewModel()
{

tblCurrentLocations = from o in rosterManagementContext.tblCurrentLocations select o;
tblStreams = from o in rosterManagementContext.tblStreams select o;
}

[Required]
public virtual IEnumerable<tblCurrentLocation> tblCurrentLocations { get; set; }

[Required]
public virtual IEnumerable<tblStream> tblStreams { get; set; }

我的局部 View :

@model PITCRoster.ViewModel.LookUpViewModel

@Html.Label("Location")
@Html.DropDownListFor(M=>M.tblCurrentLocations, new SelectList(Model.tblCurrentLocations, "LocationId", "Location"), "Select Location")
@Html.ValidationMessageFor(M=>M.tblCurrentLocations)
<br />
@Html.Label("Stream")

@Html.DropDownListFor(M => M.tblStreams, new SelectList(Model.tblStreams, "StreamId", "Stream"), "Select Streams")
@Html.ValidationMessageFor(M => M.tblStreams)

我在局部 View 上方渲染的 View

@{
ViewBag.Title = "Resources";
}
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<h2>Resources</h2>

@using (Html.BeginForm("AddResource", "Resources", FormMethod.Post))
{

@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
Html.RenderPartial("_LookUpDropDowns", new PITCRoster.ViewModel.LookUpViewModel());

<br />
<input type="submit" value="Create" />
}

这是我的 Controller 操作方法:[HttpPost]

public void AddResource(LookUpViewModel testName)
{
//code
}

当我在我的 Controller 操作方法上放置调试器时,控件会转到该操作方法。但是 ViewModel 对象中有 null。我尝试使用 FormCollection 对象访问输入的值,并且我正在按预期获取所有数据...

下面是我使用 FormCollection 进行 Controller 操作的代码

 [HttpPost]
public void AddResource(FormCollection form)
{
var x = form["tblStreams"]; //I get value here..
}

谁能解释一下为什么我没有在 ViewModel 对象中获取值?谢谢...

最佳答案

您不能将下拉列表绑定(bind)到复杂对象的集合 - 在您的情况下 IEnumerable<tblCurrentLocation>IEnumerable<tblStream>

A <select>标记仅回发一个值(所选选项的值),因此在 POST 方法中 DefaultModelBinder正试图这样做testName.tblCurrentLocations = "1" (假设所选选项的值为 1 )当然会失败并且属性设置为 null

您需要一个包含要绑定(bind)到的属性的 View 模型(理想情况下将包括 SelectList 帮助程序使用的 DropDownListFor())

public class LookUpViewModel
{
[Display(Name = "Location")]
[Required(ErrorMessage = "Please select a location")]
public int SelectedLocation { get; set; }
[Display(Name = "Stream")]
[Required(ErrorMessage = "Please select a stream")]
public int SelectedStream { get; set; }
public SelectList LocationList { get; set; }
public SelectList StreamList { get; set; }
}

然后在 View 中

@Html.LabelFor(m => m.SelectedLocation)
@Html.DropDownListFor(m => m.SelectedLocation, Model.LocationList, "-Please select-")
@Html.ValidationMessageFor(m => m.SelectedLocation)

@Html.LabelFor(m => m.SelectedStream)
@Html.DropDownListFor(m => m.SelectedStream, Model.StreamList, "-Please select-")
@Html.ValidationMessageFor(m => m.SelectedStream)

在 Controller 中

public ActionResult Edit()
{
LookUpViewModel model = new LookUpViewModel();
ConfigureViewModel(model);
return View(model);
}

[HttpPost]
public ActionResult Edit(LookUpViewModel model)
{
if (!ModelState.IsValid)
{
ConfigureViewModel(model);
return View(model);
}
// model.SelectedLocation will contain the value of the selected location
// save and redirect
}

private void ConfigureViewModel(LookUpViewModel model)
{
// populate your select lists
var locations = from o in rosterManagementContext.tblCurrentLocations select o;
model.LocationList = new SelectList(locations, "LocationId", "Location");
.... // ditto for streams
}

请注意,正如 Maximilian 的回答中所指出的,您的 View 模型应该只包含 View 所需的属性。您的 Controller 负责填充值。 View 模型永远不应该调用数据库——它甚至不应该知道数据库的存在。

关于c# - ViewModel 在操作方法中获取空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31578598/

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