表单提交获取空模型

时间:2019-05-01 16:42:56

标签: c# asp.net model-view-controller bootstrap-4

当我提交表单时,控制器方法获取空模式。我有一个Details视图,该视图从另一个控制器单击按钮调用CreateNewProduct方法。 CreateNewProduct调用SharedProduct视图或重定向到Details视图的操作方法。我在SharedProduct视图中有一个提交按钮,在提交表单时该模型为null。我可以在这里找一些助手吗?谢谢!

详细信息视图

    @model DetailsModel

    @foreach (var productTemplate in Model.Products)
     {

       @Html.ActionLink("Add New Product", "CreateNewProduct", "NewProductTemplate", new { productTemplate.productId, productTemplate.customerId}, new { @class = "btn btn-sm btn-primary" })

        @Html.ActionLink("Remove Product", "Delete", "ProductTemplate", new { productTemplate.productId}, new { @class = "btn btn-sm btn-primary" })

     }

ProdcutTemplate控制器

public ActionResult Details()
        {
            var productTemplate = _productTemplateService.GetAllProducts(User);
            return View(new DetailsModel
            {
            ProductTemplate = productTemplate,
            });
        }
public ActionResult Delete(Guid productId)
        {
            var productTemplate = _productTemplateService.Delete(productId , User);
            return View("Details",new DetailsModel
            {
            ProductTemplate = productTemplate,
            });
        }

Model SharedModel和DetailsModel

    public class SharedModel
        {
            public IEnumerable<SharedProductTemplateListItemDto> SharedProducts    {get; set; }

        }
public class DetailsModel
        {
            public IEnumerable<ProductTemplateListItemDto> Products{get; set; }

        }

NewProductTemplate控制器代码

public ActionResult CreateNewProduct(Guid productId, Guid customerId)
    {
        var sharedProduct =     _newProductTemplateService.GetSharedProductCustomer(productId);
        var _finalSharedProducts = (sharedProduct.Any(t => t.productId != productId));

        if (_finalSharedProducts)
        {
            var sharedProdctTemplate =     _newProductTemplateService.GetSharedProduct(productId);
            return View("SharedProduct", new SharedModel
            {
                SharedProducts = sharedProdctTemplate
            });
            }
           else
           {
                _productTemplateService.CreateNewProductOne(productId);
                return RedirectToAction("Details", "ProductTemplate");
        }
    }

public ActionResult ShareProduct (SharedModel shareModel)
    {
       //Access ShareProducts from shareModel 
        return RedirectToAction("Details", "ProductTemplate");
    }

视图-SharedProduct.view

    @model SharedModel
    @using (Html.BeginForm("ShareProduct", "NewProductTemplate",        FormMethod.Post))
{
            @Html.AntiForgeryToken()
   <div class="flex-row">
    <div class="col-6">
        <div class="d-flex flex-row">
            <div class="p-2">Product ID</div>
            <div class="p-2">Product Types</div>
            <div class="p-2">Status</div>
        </div>

            @for (var i = 0; i < Model.SharedProducts.Count(); i++)
            {
                @Html.HiddenFor(model => model.SharedProducts.ElementAt(i).ProductId)
                @Html.HiddenFor(model => model.SharedProducts.ElementAt(i).CustomerId)
                @Html.HiddenFor(model => model.SharedProducts.ElementAt(i).ProductType)
                @Html.HiddenFor(model => model.SharedProducts.ElementAt(i).StatusCode)
                @Html.HiddenFor(model => model.SharedProducts.ElementAt(i).IsShared)
            <div class="d-flex flex-row">
                <div class="p-2">@Html.DisplayFor(model =>   model.SharedProducts.ElementAt(i).ProductId)</div>
                   <div class="p-2">@Html.DisplayFor(model => model.SharedProducts.ElementAt(i).ProductType)</div>
                    <div class="p-2">@Html.DisplayFor(model => model.SharedProducts.ElementAt(i).StatusCode)</div>
                  @if (Model.SharedProducts.ElementAt(i).StatusCode == VersionStatus.PUBLISHED)
                {
                    <div class="p-2">@Html.EditorFor(m => m.SharedProducts.ElementAt(i).IsShared)</div>
                }
            </div>
        }
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-sm btn-primary" />
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

我很确定问题出在您引用收集项目的方式上:

.ElementAt(i)

代替尝试

@Html.HiddenFor(model => model.SharedProducts[i].ProductId)