gpt4 book ai didi

c# - viewmodel 在回发 mvc 5 上返回 null

转载 作者:行者123 更新时间:2023-12-02 14:30:42 24 4
gpt4 key购买 nike

我在 View 模型方面遇到了这个问题,当发布 View 时,我得到所有属性的空值。

我进行了一些研究,我发现很多人都有这个问题,我尝试了一些解决方案,但没有一个适合我。

这就是我所拥有的:

View 模型:

public class EventViewModel
{
public projectEvent ProjectEvent { get; set; }
public List<eventType> eventTypes { get; set; }
}

Controller :

[HttpGet]
public ActionResult AddEditEvent(int? id)
{
var eventViewModel = new EventViewModel();
var projectEventModel = new projectEvent();
if (id != null)
{
using (var db = new DBEntities())
{
projectEventModel = (from p in db.projectEvents
where p.eventID == id
select p).FirstOrDefault();
}
}

eventViewModel.ProjectEvent = projectEventModel;

using (var db = new DBEntities())
{
eventViewModel.eventTypes = (from p in db.eventTypes
select p).ToList();
}

return View(eventViewModel);
}

[HttpPost]
public ActionResult AddEditEvent(EventViewModel projectEvent)
{
if (ModelState.IsValid)
{
using (var db = new DBEntities())
{
db.projectEvents.AddOrUpdate(projectEvent);
db.SaveChanges();
}
}
return RedirectToAction("Events");
}

查看:

@model TTB.ViewModels.EventViewModel

@{
Layout = "~/Views/Shared/_Layout_Main.cshtml";
}

<h2>Add/Edit Event</h2>

@using (Html.BeginForm("AddEditEvent", "Admin"))
{
<div class="container">
<div class="col-md-8">
<div class="form-group">
<label for="name">Name</label>
@Html.TextBoxFor(m => m.ProjectEvent.eventName, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.ProjectEvent.eventName)
</div>
<div class="form-group">
<label for="name">Date</label>
@Html.TextBoxFor(m => m.ProjectEvent.eventDate, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.ProjectEvent.eventDate)
</div>
<div class="form-group">
<label for="name">Event Type</label>
@Html.DropDownListFor(v => v.ProjectEvent.eventType, new SelectList(Model.eventTypes, "eventTypeID", "eventTypeName"), new { @class = "form-control dropdown" })
</div>
@Html.HiddenFor(m => m.ProjectEvent.eventID)
</div>
<div class="col-md-8">
<input type="submit" class="btn btn-success btn-lg btnSaveEdit" value="Save" />
</div>
</div>
}

我做错了什么?

更新

渲染的 html 输出:

<form action="/Admin/AddEditEvent" method="post">    <div class="container">
<div class="col-md-8">
<div class="form-group">
<label for="name">Name</label>
<input class="form-control" data-val="true" data-val-required="The eventName field is required." id="ProjectEvent_eventName" name="ProjectEvent.eventName" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="ProjectEvent.eventName" data-valmsg-replace="true"></span>
</div>
<div class="form-group">
<label for="name">Date</label>
<input class="form-control" data-val="true" data-val-date="The field eventDate must be a date." data-val-required="The eventDate field is required." id="ProjectEvent_eventDate" name="ProjectEvent.eventDate" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="ProjectEvent.eventDate" data-valmsg-replace="true"></span>
</div>
<div class="form-group">
<label for="name">Event Type</label>
<select class="form-control dropdown" id="ProjectEvent_eventType" name="ProjectEvent.eventType"><option value="1">movie</option>
</select>
</div>
<input data-val="true" data-val-number="The field eventID must be a number." data-val-required="The eventID field is required." id="ProjectEvent_eventID" name="ProjectEvent.eventID" type="hidden" value="0" />
</div>
<div class="col-md-8">
<input type="submit" class="btn btn-success btn-lg btnSaveEdit" value="Save" />
</div>
</div>
</form>

最佳答案

这是因为您的模型上有一个名为 projectEvent 的属性,并且当将模型传回 Controller 时,您将模型称为 projectEvent

改变

public ActionResult AddEditEvent(EventViewModel projectEvent)

public ActionResult AddEditEvent(EventViewModel model)

(或您选择的其他名称)

http://tech.pro/blog/1930/don-t-name-a-model-the-same-as-a-property-you-re-trying-bind-in-aspnet-mvc

关于c# - viewmodel 在回发 mvc 5 上返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27191691/

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