2016-05-30 119 views
1

以下是我的场景:我有一个名为MaterialPaymentRequest的模型。它由几个MaterialPaymentRequestSubItem组成,因此PaymentRequest是父项,MaterialPaymentRequestSubItem是它的子项。 考虑当我有一个MaterialPaymentRequest,我想给它添加一个孩子。 目前其内部MaterialPaymentRequestSbuItemController方法是这样的:ASP.Net MVC:将信息传递给HTTPost控制器

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) 
    { 
     ... 
    } 
    .... 
} 

我见过像this方法,其使用Html.Hidden,但我认为这是不够安全,因为用户可以操纵在用户侧信息。

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

我可以将信息传递给接受parentID作为参数的控制器吗?

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

回答

0

为了只在客户端阅读HTML辅助输入控制,使用以下命令:

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

我建议你保持在服务器端parentId的内容状态Session变量,如果你不想要使用客户端隐藏字段。

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。

相关问题