如何通过单击编辑按钮使用mvc4 devExpress中的SetDataItemTemplateContent将参数从gridview传递到控制器?

时间:2017-01-17 17:38:56

标签: c# asp.net-mvc-4 gridview devexpress

您好我有一个注册页面,它包含名为UID,UserName,Password的字段。我正在使用dev Express gridview。当我单击编辑按钮时,它需要将特定的行键值传递给控制器​​,但它将null参数传递给控制器​​。现在我想要的是我想通过使用SetDataItemTemplateContent.below mvc dev express gridview中的网格视图中的编辑按钮将特定行键参数传递给控制器​​我发布我的代码请任何人更正我的代码并告诉我我做了什么错误。

我的模型(UserMasterViewModel)

 public int UID{get;set;}
 public string UserName{get;set;}
 public string Password{get;set;}

我的控制器代码

Public ActionResult UserMasterEdit(int? id)
{
 View_MUsrRegistration  userregistrartion = db.MUsrRegistration.Find(id)
 UserMasterViewModel usermasterviewmodel = new UserMasterViewModel();

  usermasterviewmodel.UserName = userregistrartion.UserName;
  usermasterviewmodel.Password = userregistrartion.Password; 
  return View(usermasterviewmodel)
}

网格视图代码

@Html.DevExpress().GridView(
  settings =>
   {
    settings.Name = "gvEditing";
    settings.KeyFieldName = "UID";
    settings.CallbackRouteValues = new { Controller = "UserMaster", Action = "UserMasterPartial" };
    settings.Width = Unit.Percentage(100);

    settings.Columns.Add(column => {
        column.Caption = "#";
        column.SetDataItemTemplateContent(c =>
        {
            ViewContext.Writer.Write(
                Html.ActionLink("Edit", "UserMasterEdit", new { UID = DataBinder.Eval(c.DataItem, "UID") }) + " " +
                Html.ActionLink("Delete", "UserMasterDelete", new { ProductID = DataBinder.Eval(c.DataItem, "UID") },
                    new { onclick = "return confirm('Do you really want to delete this record?')" })
            );
        });

        column.Settings.AllowDragDrop = DefaultBoolean.False;
        column.Settings.AllowSort = DefaultBoolean.False;
        column.Width = 70;
    });
    settings.Columns.Add("UserName");
    settings.Columns.Add("Password");
    settings.ClientLayout = (s, e) =>
    {
        if(e.LayoutMode == ClientLayoutMode.Loading) {
            if(Session["GridState"] != null)
                e.LayoutData = (string)Session["GridState"];
        }
        else
            Session["GridState"] = e.LayoutData;
    };
}).Bind(Model).GetHtml()

编辑视图

@model MSSERP.Models.UserMasterViewModel
 @{
  Html.EnableClientValidation(); 
}

 @using(Html.BeginForm("UserMaterUpdate", "UserMaster", FormMethod.Post, new { @class = "edit_form" })) {
@Html.HiddenFor(m=>m.UID)
<div class="line">
   @Html.Label(UserNmae)
   @Html.TextBoxFor(m=>m.UserName)
</div>

<div class="line">
   @Html.Label(Password)
   @Html.TextBoxFor(m=>m.Password)
</div>
<div class ="line">
<input type="submit" value ="Save"/>
</div>

在此我点击编辑按钮,它将空值传递给控制器​​。我尝试了很多方法,但无法将UID参数传递给控制器​​。 我不知道我在这次行动中犯了什么错误。我尽力解释这个问题任何人都理解并帮助我解决这个问题。

谢谢..

1 个答案:

答案 0 :(得分:1)

尝试以下&#39;

变化:

Html.ActionLink("Edit", "UserMasterEdit", new { UID = DataBinder.Eval(c.DataItem, "UID") }) 

要:

Html.ActionLink("Edit", "UserMasterEdit", new { id = DataBinder.Eval(c.DataItem, "UID") }, null) 

参数名称应与控制器的参数名称匹配。 需要添加null参数以确保调用正确的解析方法。请看这个链接:HTML.ActionLink method