- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以,我有一个 Controller / View / View 模型的问题。它类似于 this issue我认为。基本上,我有一个从我的 Controller 发送到 View 的 View 模型。在将整个困惑发送回 Controller 后操作之前,有些项目会显示,然后是一些额外的字段供用户操作。当我在帖子中取回数据时,所有 View 模型都是空的。
所以,事不宜迟,这里有一些代码要看:
View 模型:
public class ASideReceivingViewModel
{
public PurchaseOrderLine poLine;
public ReceivingItem receivingItem;
public Dictionary<string, string> TankerOrRailcarOptions { get; set; }
public ASideReceivingViewModel()
{
TankerOrRailcarOptions = new Dictionary<string, string>();
TankerOrRailcarOptions.Add("R", "Railcar");
TankerOrRailcarOptions.Add("T", "Tanker");
}
}
Controller 操作:
public ActionResult Receive(string strOrdNo, short? shtLineNo)
{
//if there isn't a selected po line, then shoot them back to the first page
if (strOrdNo == null || !shtLineNo.HasValue) return RedirectToAction("Index");
PurchaseOrderService poService = new PurchaseOrderService();
ReceivingItemService s = new ReceivingItemService(p);
ASideReceivingViewModel vm = new ASideReceivingViewModel();
vm.poLine = poService.GetOpenPurchaseOrderLines().Where(po => po.Ord_no == strOrdNo &&
po.Line_no == shtLineNo).FirstOrDefault();
if (vm.poLine == null) return RedirectToAction("Index");
vm.receivingItem = s.CreateNewReceivingItem(vm.poLine);
return View(vm);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Receive(ASideReceivingViewModel mytestvm)
{
if (ModelState.IsValid && mytestvm.receivingItem != null)
{
ReceivingItemService s = new ReceivingItemService(p);
s.Update(mytestvm.receivingItem);
return RedirectToAction("Index");
}
return View(mytestvm);
}
查看:
@model FSIApps.Presentation.Models.ASideReceivingViewModel
<div class="row">
@{Html.RenderPartial("POLineDetails", Model.poLine);}
</div>
@using (Html.BeginForm("Receive", "Receiving", FormMethod.Post))
{
@Html.HiddenFor(model => model.receivingItem.Id)
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="row">
@Html.AntiForgeryToken()
<div class="col-md-6">
<div class="form-group">
<label for="receivingItem_Batch_number">Batch Number</label>
@Html.TextBoxFor(model => model.receivingItem.Batch_number, new { @class = "form-control" })
<span class="help-block">*Also the Vendor Lot Number on the BOL</span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="receivingItem_Qty_received">Qty Received</label>
@Html.TextBoxFor(model => model.receivingItem.Qty_received, new { @class = "form-control" })
<span class="help-block">*Qty shown on BOL</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="receivingItem_Carrier">Carrier</label>
@Html.TextBoxFor(model => model.receivingItem.Carrier, new { @class = "form-control" })
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="receivingItem_">Tanker or Railcar</label>
@Html.DropDownListFor(m => m.receivingItem.Tanker_or_railcar, new SelectList(Model.TankerOrRailcarOptions, "Key", "Value", Model.receivingItem.Tanker_or_railcar), new { @class = "form-control" })
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="receivingItem_Railcar_number">Railcar Number</label>
@Html.TextBoxFor(model => model.receivingItem.Railcar_number, new { @class = "form-control" })
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="receivingItem_Manifest_number">Manifest Number</label>
@Html.TextBoxFor(model => model.receivingItem.Manifest_number, new { @class = "form-control" })
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="submit" value="Save" class="btn btn-success" />
</div>
</div>
</div>
</div>
</div>
}
我不一定关心我发送到局部 View 的数据,但是当我回发常规表单时,我没有在 ViewModel 中设置任何内容。在另一篇文章中,他们讨论了命名发送回 Controller 的参数的问题,但是在我的 @Html.BeginForm()
中设置值的组合似乎没有用。
这里有人对我有什么建议吗?
编辑:
最佳答案
要使用自动模型绑定(bind),您应该使用属性而不是 View 模型中的字段。希望这可以解决问题:
public class ASideReceivingViewModel
{
public PurchaseOrderLine poLine { get; set; };
public ReceivingItem receivingItem { get; set; };
...
}
关于c# - MVC5 ViewModel 不回发到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20427126/
你能定义自动回发和回发吗?它们的区别,哪些Web服务器控件支持自动回发和回发,以及回发和回调之间的区别? 最佳答案 回发:页面向自身执行 POST 请求 autopostback:自动执行回发,例如当
我有一个 ASP.NET 表单,其中包含一个下拉列表,它会在更改时发回服务器,并用一些日期填充第二个下拉列表。 该表单还包含其他字段,其中一些字段在客户端进行验证,另一些字段在服务器端进行验证。 这是
我需要在我的 ASPX 页面中使用 JavaScript 确认函数来确认基于 C# 代码中条件的各种操作,而不是直接连接到按钮单击事件。例如。如果计算出记录数 > 200,则询问“你想继续吗?”然后根
我正在尝试为一个项目构建一个非常具体的搜索页面,但我在处理由单个页面上动态生成的控件调用的多个回发时遇到了很多麻烦。 页面必须像这样工作: 有一个复选框,“详细搜索”,会在选中/取消选中时引起回发。
在 React 组件中如何防止它回发,以便在按下提交按钮时页面不会重新加载。 组件代码如下: const class MyComponent extends React.Component { .
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: prevent postback of HtmlButton in C# 这是我的 JavaScript 函数:
我们有一个 ASP.NET MVC3 网络应用程序。我们生成 HTML5 页面。 现在,我们希望将拖放内容(图片、word文档...)发送到HTML5页面,然后发送到Web服务器在 C# 中处理(内容
我正在使用 jquery tab 并遵循 js 方法,如何以及如何修改它以在回发之间保持 tab 状态? (这会将选项卡重置为 page_load 后的第一个选项卡) $(document).read
我正在 ASP.NET 4.0 网站中使用 AjaxFileUpload。问题是,当我上传文件时,它的 UploadComplete 会触发,从而导致回发到页面。在由 AjaxFileUpload 引
我在表单上有三个单选按钮 - A、B、C。这些选择中的每一个都会使用特定于该选项的数据填充一个下拉列表。加载表单时,我将选项 A 设置为选中(默认)。 当我选择按钮 B 或 C 时,AsyncPost
我正在尝试使用 JQuery 阻止页面回发。 $(document).ready(function() { $('#').click(function() {
这个问题是一个更大页面的一部分,但我简化了代码以提供一个简单的示例来说明我正在谈论的内容。基本上我有一个带有 AutoPostBack="true"的 ASP 下拉列表,我想做一些 javascrip
我在页面上有一个 AJAX 请求。 有没有办法找到在页面上触发 AJAX 调用的时间。在 jQuery 或 javascript 中查找或发起一个函数,在页面上调用 AJAX 请求。 最佳答案 参见
我有一个按钮可以通过回发提交我的表单,我想进行一些评估,如果一切都不好显示错误消息并停止回发。我已设法让我的代码进入我的 onsubmit 函数,但无法停止回发。 表格: 功能: function
我正在混合使用 asp.net 网络表单和 asp.net-mvc。要使用我包含的网络表单 routes.IgnoreRoute("Reports/{*pathInfo}"); 在 public st
我的网页中有文本框,其中有RequiredFieldValidators。这些控件的最底部是按钮提交和清除每次我单击提交按钮时,它都会触发来自RequiredFieldValidators的验证,这很
我有一个用 jquery 做的键盘控制菜单(当我按下时,它标记第二个选项,依此类推...) 但是当我选择我需要的选项后,我想单击 Enter 来调用按钮的 OnClick 方法,就像用户真的单击了它一
我有一个表单,用于更新数据库中的一组现有信息。其中有一个选择框,当选择选项“B”时,它会显示一个隐藏部分,而在选项“A”上,它将隐藏该部分。这很好用。 但是,当我从数据库加载记录集并且选择框设置为选项
我有一个 asp.net 项目,我们称它们为对象。所有对象都在转发器中创建: '/>
我有一个 ModalPopup,它将包含一个 gridview 和 4 个用于将项目输入到 gridview 本身的字段。 是否可以在保持模式打开的同时回发到服务器并更新 gridview? 当您提交
我是一名优秀的程序员,十分优秀!