我陷入了一个从视图返回到具有相关数据的控制器的问题,每次我提交具有相关数据的表单时,相关数据都会为空。 (OrderRows)

我检查了Chorme开发工具,发现我确实将OrderRows发布为row.Id,但可以在Visual Studio中将它们视为变量。

班级

public class OrderModel
{
   [Key]
   public int Id { get; set; }
   public int OrderID { get; set; }
   public List<OrderRow> OrderRows { get; set; }
}

public class OrderRow
{
    [Key]
    public int Id { get; set; }
    public int OrderID { get; set; }
    public int OrderRowID { get; set; }
    public OrderModel OrderModel { get; set; }

}


编辑控制器(GET)

public ActionResult Edit(int? id)
{
    OrderModel orderModel = db.OrderModel.Where(b => b.Id == id).Include(b => b.OrderRows).FirstOrDefault();
    return View(orderModel);
}


交付的控制者(过帐,在提交表单时被激活)

public ActionResult Delivered(OrderModel orderModel, FormCollection formCollection)
{
        //db.Entry(orderModel).State = EntityState.Modified;
        //Set state to Delivered
        //db.SaveChanges();
        return RedirectToAction("Index");
}


编辑视图

using (Html.BeginForm())
{

    @Html.LabelFor(model => model.OrderID): @Html.DisplayFor(model => model.OrderID)
    @Html.HiddenFor(model => model.OrderRows)




<div class="row">
    @Html.Label("Delivery information")
    <br />
    @Html.HiddenFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })
    @Html.DisplayFor(model => model.Id, "", new { @class = "text-danger" })
    <br />

    @Html.HiddenFor(model => model.OrderID, new { htmlAttributes = new { @class = "form-control" } })
    @Html.DisplayFor(model => model.OrderID, "", new { @class = "text-danger" })
    <br />
</div>

<table class="table table-striped table-bordered">
    @foreach (var row in Model.OrderRows)
    {
        <tr>
            <td>
                <input type="checkbox" value="@row.OrderRowID">
            </td>
            <td>
                @Html.HiddenFor(model => row.Id, new { htmlAttributes = new { @class = "form-control" } })
                @Html.DisplayFor(model => row.Id, "", new { @class = "text-danger" })
            </td>
            <td>
                @Html.HiddenFor(model => row.OrderID, new { htmlAttributes = new { @class = "form-control" } })
                @Html.DisplayFor(model => row.OrderID, "", new { @class = "text-danger" })
            </td>
            <td>
                @Html.HiddenFor(model => row.OrderRowID, new { htmlAttributes = new { @class = "form-control" } })
                @Html.DisplayFor(model => row.OrderRowID, "", new { @class = "text-danger" })
            </td>
            <td>
                @Html.HiddenFor(model => row.OrderModel, new { htmlAttributes = new { @class = "form-control" } })
                @Html.DisplayFor(model => row.OrderModel, "", new { @class = "text-danger" })
            </td>
        </tr>

    }
    </table>
        <div class="form-group">
                <input type="submit" value="Mark as Delivered" formaction="/OrderModels/Delivered" class="btn btn-info" />
    </div>
}

最佳答案

我认为您想要使用OrderRow代替OrderModel。如果我了解您的需求,则希望获得(一个)OrderModel并包含(许多)OrderRows。如果这是您想要的,则应在视图中使用OrderRow作为模型:

OrderRow orderRows = db.OrderRow.Where(b => b.OrderModel.Id == id).ToList();

return View(orderRows );


然后,您想在视图中更改模型:

@model IEnumberable<OrderRow>


这样,您可以拥有所有的OrderRows,包括其OrderModel

关于c# - Asp.net-更新相关数据-提交时获取null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31744501/

10-11 01:51