繁体   English   中英

ASP.Net MVC:将信息传递给HTTPost Controller

[英]ASP.Net MVC: pass information to HTTPost Controller

这是我的场景:我有一个名为MaterialPaymentRequest的模型。 它由几个MaterialPaymentRequestSubItem组成,因此PaymentRequest是父级, MaterialPaymentRequestSubItem是其子级。 考虑什么时候我有一个MaterialPaymentRequest ,我想要添加一个子项。 目前, MaterialPaymentRequestSbuItemController方法如下所示:

public ActionResult CreateChild(int parentId)
{
    if (parentId==null)
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

    var parenetRequest = (from request in db.MaterialPaymentRequests
        where request.Id==parentId
        select request);

    ViewBag.MaterialPaymentRequestId = new SelectList(parenetRequest, "Id", "Description", parentId);
    ViewBag.ParentID = parentId;
    return View();
}

我的问题是在视图内部,用户可以更改其父级,因为我有一个下拉列表,我无法冻结它或使其只读:

@Html.DropDownList("MaterialPaymentRequestId", String.Empty)

我已经尝试使用ViewModel并在发布后我设置了我的chil的parentID但是这样我不知道如何将ParentId传递给http-post控制器方法。

使用ViewMode之前的回发方法是这样的:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateChild([Bind(Include = "Id,Name,Unit,UnitPrice,MaterialPaymentRequestId,Quantity")] MaterialPaymentRequestSubItem materialpaymentrequestsubitem)
{
    if (ModelState.IsValid)
    {
        ...
    }
    ....
}

我见过像这样的方法使用Html.Hidden但我认为它不够安全,因为用户可以在用户端操作信息。

有没有更好的方法来做到这一点?

我可以将信息传递给接受parentID作为参数的控制器,并带有这样的声明吗?

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateChild(int parentID, [Bind(Include = "Id,Name,Unit,UnitPrice,MaterialPaymentRequestId,Quantity")] MaterialPaymentRequestSubItem materialpaymentrequestsubitem)
{
    if (ModelState.IsValid)
    {
        ...
    }
    ....
}

要使HTML帮助器输入控件只在客户端读取,请使用以下命令:

@Html.DropDownList("MaterialPaymentRequestId", String.Empty, new { @readonly = "readonly" })

如果您不想使用客户端隐藏字段,我建议您使用Session变量维护服务器端的parentId内容状态。

Session["parentId"] = parentId;

// just an example to extract Session variable
int parentId = Session["parentId"] != null ? Convert.ToInt32(Session["ParentId"]) : return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

但是,如果您需要视图中的隐藏字段,请使用隐藏字段中的编码文本和验证模式(例如Base64或更好的编码),因此访问您站点的任何用户都无法轻易更改客户端的值。

视图:

@Html.Hidden("ParentID", @ViewBag.ParentID);

// or if you have a viewmodel, pass viewmodel's value here
@Html.HiddenFor(model => model.ParentID);

控制器方法:

public ActionResult CreateChild(int parentId)
{
    ...
    // convert parentId into Base64
    ViewBag.ParentID = Convert.ToBase64String(parentId);
    return View(ViewBag); // model binding
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateChild([Bind(Include = "Id,Name,Unit,UnitPrice,MaterialPaymentRequestId,Quantity,ParentID")] MaterialPaymentRequestSubItem materialpaymentrequestsubitem)
{
     ...
     // get Parent ID
     int parentId = (int)Convert.FromBase64String(ParentID);

     // write your own algorithm to validate hidden field's value
     ...
     if (ModelState.IsValid) 
     {
          // redirect to create child elements
          return RedirectToAction("CreateChild", "Controller", new { @id = parentId });
     }
}

希望这个解释对你有用,CMIIW。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM