【问题标题】:MVC5 ViewModel Not Posting back to ControllerMVC5 ViewModel 未回发到控制器
【发布时间】:2013-12-06 15:05:12
【问题描述】:

所以,我遇到了控制器/视图/视图模型的问题。我认为它类似于this issue。基本上,我有一个视图模型,我从控制器发送到视图。在整个混乱被发送回控制器发布操作之前,有一些项目会显示,然后是一些额外的字段供用户操作。当我在帖子中取回数据时,所有的视图模型都是空的。

所以,事不宜迟,下面是一些代码:

视图模型:

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");

    }

}

控制器操作:

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>
}

我不一定关心我发送到局部视图的数据,但是当我发回常规表单时,我在 ViewModel 中没有设置任何内容。在另一篇文章中,他们讨论了如何将参数命名为发送回控制器的问题,但在我的 @Html.BeginForm() 中设置值似乎没有任何组合可以解决问题。

这里有人对我有什么建议吗?

已编辑:

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-viewmodel


    【解决方案1】:

    要使用自动模型绑定,您应该在视图模型中使用属性而不是字段。希望这能解决问题:

    public class ASideReceivingViewModel
    {
        public PurchaseOrderLine poLine { get; set; };
        public ReceivingItem receivingItem { get; set; };
        ...
    }
    

    【讨论】:

    • 就是这样!谢谢!
    • 一直在寻找这个大约 7 小时...只有在我开始问自己的问题时才出现:(
    • 完美答案!我希望我能早 3 小时找到它;-) 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多