简体   繁体   English

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

[英]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.您好,我有一个注册页面,它包含名为 UID、用户名、密码的字段。 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.我正在使用 dev Express gridview。当我单击编辑按钮时,它需要将特定的行键值传递给 controller,但它会将 null 参数传递给 controller。现在我想要的是通过单击将特定的行键参数传递给 controller使用 SetDataItemTemplateContent 在 mvc dev express gridview 中的网格视图中编辑按钮。下面我发布我的代码,请任何人更正我的代码并告诉我我犯了什么错误。

My Model (UserMasterViewModel)我的 Model (UserMasterViewModel)

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

My Controller Code我的 Controller 密码

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.在此我单击编辑按钮,它将 null 值传递给 controller。我尝试了很多方法但无法将 UID 参数传递给 controller。我不知道我在这个操作中犯了什么错误。 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. 需要添加null参数以确保调用正确的解析方法。 see this link: HTML.ActionLink method 看到这个链接: HTML.ActionLink方法

In the GridView where your DisplayFor's are located:在您的 DisplayFor 所在的 GridView 中:

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

Or use an ActionLink:或者使用 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. id 变量将包含您从网格视图传入的值。 From there, you can populate your model and show it.从那里,您可以填充您的 model 并显示它。

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

相关问题 我如何使用 datapost 将参数从 jqgrid 传递到 controller(使用 MVC4 和 asp.net) - how can i pass parameter from jqgrid to controller with datapost (using MVC4 and asp.net ) 如何将xml从控制器传递到mvc4中的其他控制器 - how to pass xml from controller to other controller in mvc4 如何通过清单 <model> 从控制器在MVC4中查看? - how to pass List<model> to controller from view in MVC4? 如何在mvc4中从控制器传递一对多到视图 - how to pass one-to-many from controller to view in mvc4 如何将参数从MVC4应用程序传递给辅助角色? - how to pass parameter from MVC4 application to worker role? 如何在MVC4中不使用Ajax的情况下将参数从View传递到Controller? 有可能吗? - How to pass parameters from View to Controller without using Ajax in MVC4?Is it possible? MVC4将Lambda从控制器传递到存储库 - MVC4 pass lambda from controller to repository 如何在MVC4中的控制器中更改参数名称? - How to change parameter name in controller in MVC4? 单击 ASP.NET MVC 中的按钮时,如何将 model 值从视图传递到 controller - How to pass model value from a view to a controller when clicking on the button in ASP.NET MVC 如何使用devexpress中的按钮插入gridview? - How to insert into gridview by using button in devexpress?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM