我有一个非常简单的问题,可能有一个非常简单的答案,但是,我无法解决。
我有一个视图模型如下:
namespace CommunicationsLog.ViewModels
{
public class CommunicationViewModel
{
public Communication Communication { get; set; }
public Feedback Feedback { get; set; }
public IEnumerable<Resolution> Resolutions { get; set; }
}
}
我正在尝试从Add.cshtml页面创建一个新的通信实例:
@model CommunicationsLog.ViewModels.CommunicationViewModel
@{
ViewBag.Title = "Add";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("Add", "Communication"))
{
<div class="form-horizontal" id="addForm">
<div class="row">
<div class="col-md-12">
@Html.ValidationSummary(true, "", new { @class="text-danger"})
<div class="form-group">
@Html.LabelFor(model => model.Communication.Customer.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@if (Model.Communication.Customer.Name == null)
{
<input id="btnCust" type="button" value="Select Customer" class="btn btn-default addCustomer" />
}
else
{
<span id="custSpan" style="font-size:16px; font:bold;">
@Html.DisplayFor(model => model.Communication.Customer.Name)
<input id="btnEditCust" type="button" value="Edit" class="btn btn-default addCustomer"/>
</span>
}
<div id="addCust"></div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input id="btnSubmit" type="submit" value="Create" class="btn btn-default" />
</div>
</div>
}
<div>
@Html.ActionLink("Back to Communications", "Index")
</div>
在提交时,调用以下控制器操作:
[HttpPost]
public ActionResult Add(CommunicationViewModel viewModel)
{
callEnd = DateTime.Now;
var totalCallTime = callEnd - callStart;
Communication insertedComm = null;
try
{
Communication comm = viewModel.Communication;
comm.CallTime = totalCallTime.ToString(@"mm\:ss");
var customer = _uow.CustomerRepository.Get()
.Where(c => c.Name == comm.Customer.Name);
comm.CustomerId = customer.FirstOrDefault().CustomerId;
comm.Customer = customer.FirstOrDefault();
comm.State = "Open";
if (ModelState.IsValid)
{
insertedComm = _uow.CommunicationRepository.Insert(comm);
_uow.Save();
}
Feedback feedback = viewModel.Feedback;
if (feedback.Type != null && feedback.Notes != null)
{
feedback.Communication = insertedComm;
feedback.CommunicationId = insertedComm.CommunicationId;
var insertedFB = _uow.FeedbackRepository.Insert(feedback);
_uow.Save();
}
return RedirectToAction("Index");
}
catch (Exception e)
{
if(viewModel.Communication == null)
{
viewModel.Communication = new Communication();
}
return View(viewModel);
}
}
但是,当viewModel加载到控制器操作时,通信对象为null。反馈对象已成功实例化。
在调试之后,我已经确定视图模型是正确实例化的,并且在页面加载时不是null,并且正确显示预定义数据。它似乎只会丢失POST请求的值。
有人可以帮忙吗?
非常感谢,
编辑:
正如评论中指出的那样,我实际上已从我的代码段中删除了重要部分,因此这里是不包含javascript的完整视图,因为它与POST请求无关:
@model CommunicationsLog.ViewModels.CommunicationViewModel
@{
ViewBag.Title = "Add";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Add</h2>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
@using (Html.BeginForm("Add", "Communication"))
{
<div class="form-horizontal" id="addForm">
<h4>Communication</h4>
<div id="countdown" class="countdownHolder"></div>
<hr/>
<div class="row">
<div class="col-md-12">
@Html.ValidationSummary(true, "", new { @class="text-danger"})
<div class="form-group">
@Html.LabelFor(model => model.Communication.Customer.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@if (Model.Communication.Customer.Name == null)
{
<input id="btnCust" type="button" value="Select Customer" class="btn btn-default addCustomer" />
}
else
{
<span id="custSpan" style="font-size:16px; font:bold;">
@Html.DisplayFor(model => model.Communication.Customer.Name)
<input id="btnEditCust" type="button" value="Edit" class="btn btn-default addCustomer"/>
</span>
}
<div id="addCust"></div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Communication.Receiver, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Communication.Receiver, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Communication.Receiver, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Communication.Department, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Communication.Department, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Communication.Department, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Communication.CommDateTime, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<span id="custSpan" style="font-size:14px;">
@Html.DisplayFor(model => model.Communication.CommDateTime)
</span>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Communication.Method, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Communication.Method, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Communication.Method, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Communication.Product, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Communication.Product, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Communication.Product, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Communication.PartNo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Communication.PartNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Communication.PartNo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Communication.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Communication.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Communication.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.Label("Feedback?", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="checkbox" id="chkFeedback" name="chkFeedback" onclick="showFeedbackForm(this)"/>
</div>
</div>
<div id="feedbackForm" style="display:none">
<div class="form-group">
@Html.LabelFor(model => model.Feedback.Type, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Feedback.Type, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Feedback.Type, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Feedback.Notes, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Feedback.Notes, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Feedback.Notes, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input id="btnSubmit" type="submit" value="Create" class="btn btn-default" />
</div>
</div>
}
<div>
@Html.ActionLink("Back to Communications", "Index")
</div>
我不知道它是否有帮助,但这里是来自VS的完整堆栈跟踪:
Object reference not set to an instance of an object.
at ASP._Page_Views_Communication_Add_cshtml.Execute() in C:\Projects\CommunicationsLog\CommunicationsLog\Views\Communication\Add.cshtml:line 24
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.StartPage.RunPage()
at System.Web.WebPages.StartPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
答案 0 :(得分:1)
由于您使用的是标记,因此任何<input>
标记都需要定义一个名称,输入的名称/值对将被发布回服务器:
<input name="@Html.NameFor(i => i.Communication.Customer.Name)" ... />
然后将填充模型。你也可以使用:
@Html.TextBoxFor(i => i.Communication.Customer.Name)
您的所有输入都需要这个。我还看到你的value属性包含“Select Customer”;这将发回服务器。如果您使用placeholder="Select Customer"
,则此值不会发布到服务器,但仍会在文本框为空时显示。