简体   繁体   中英

How to pass Parameter from gridview to controller using SetDataItemTemplateContent in mvc4 devExpress by clicking Edit Button?

Hi i have one registration page and it contain fields called UID,UserName,Password. I am using dev Express gridview. When i click the edit button it need to pass the particular row key value to controller but it passing null parameter to controller. Now what i want is i want to pass the particular row key Parameter to controller by clicking edit button in grid view in mvc dev express gridview using SetDataItemTemplateContent.Below i post my code please any one correct my code and tell me what mistake i did.

My Model (UserMasterViewModel)

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

My Controller Code

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

Grid View Code

@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()

EDIT View

@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>

In this i am clicking edit button it passing the null value to controller. I tried many ways but cant able to pass the UID parameter to controller. i donno what mistake i did in this action. I tried my level best to explain this issue Any one understand and help me to resolve this issue.

Thanks..

Try the following'

Change:

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

To:

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

The parameter name should match the controller's parameter name. The added null argument is needed to ensure that the right parsing method is called. see this link: HTML.ActionLink method

In the GridView where your DisplayFor's are located:

<a class="ml-2" href="/Merchant/Merchant_Boarding/@Html.DisplayFor(modelItem => item.GUID)" title="Edit">Edit</a>

Or use an ActionLink:

@Html.ActionLink("Edit", "Merchant_Boarding", new { id = (item.GUID) }, new { @class = "ml-2" })

In your Get:

        [HttpGet]
    public IActionResult Merchant_Boarding(string? id)
    {
        var model = new MerchantDetail();
        MerchantData merchdata = new MerchantData();

        model = merchdata.RetrieveMerchantData(id);
        merchdata.Dispose();
        return View(model);
    }

The id variable will contain the value you passed in from the grid view. From there, you can populate your model and show it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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