MVC5 ViewModel未发回控制器

 岁月掌心_447 发布于 2023-02-12 13:52

所以,我有一个控制器/视图/视图模型的问题.我认为它类似于这个问题.基本上,我有一个viewmodel,我发送到我的控制器的视图.在将整个混乱发送回控制器后期操作之前,有些项目会显示,然后是一些其他字段供用户操作.当我在帖子中获取数据时,所有viewmodel都是空的.

所以,不用多说了,这里有一些代码可供参考:

视图模型:

public class ASideReceivingViewModel
{
    public PurchaseOrderLine poLine;
    public ReceivingItem receivingItem;

    public Dictionary TankerOrRailcarOptions { get; set; }

    public ASideReceivingViewModel()
    {
        TankerOrRailcarOptions = new Dictionary();
        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
@{Html.RenderPartial("POLineDetails", Model.poLine);}
@using (Html.BeginForm("Receive", "Receiving", FormMethod.Post)) { @Html.HiddenFor(model => model.receivingItem.Id)
@Html.AntiForgeryToken()
@Html.TextBoxFor(model => model.receivingItem.Batch_number, new { @class = "form-control" }) *Also the Vendor Lot Number on the BOL
@Html.TextBoxFor(model => model.receivingItem.Qty_received, new { @class = "form-control" }) *Qty shown on BOL
@Html.TextBoxFor(model => model.receivingItem.Carrier, new { @class = "form-control" })
@Html.DropDownListFor(m => m.receivingItem.Tanker_or_railcar, new SelectList(Model.TankerOrRailcarOptions, "Key", "Value", Model.receivingItem.Tanker_or_railcar), new { @class = "form-control" })
@Html.TextBoxFor(model => model.receivingItem.Railcar_number, new { @class = "form-control" })
@Html.TextBoxFor(model => model.receivingItem.Manifest_number, new { @class = "form-control" })
}

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

有人对我有什么建议吗?

编辑:

Visual Studio Break

提琴手数据

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有