ASP.NET MVC3 WebGrid format: parameter

后端 未结 3 915
北海茫月
北海茫月 2021-01-04 09:23

I am trying to use the new WebGrid in ASP.NET MVC3 and one of the columns I want to display set of link icons that performs various actions (Edit, View, Delete)... For this

相关标签:
3条回答
  • 2021-01-04 09:31
    grid.Column(format: (item) => Html.ActionLink("EditCustomer","Editcustomer", new {CustomerId=item.CustomerId}))
    

    This will work ,and above one also work.. Here 1st "EditCustomer" is the text to be visible to users , second "EditCustomer" is the name of action you want it to redirect , and item is

    0 讨论(0)
  • 2021-01-04 09:47

    I had to create a WebGrid in C# code and had to display a specific column using hyperlink. None of the techniques mentioned above worked for me. So, I used MvcHtmlString.create and passed in the return value of String.format to it.

    
      format: (item) => MvcHtmlString.Create(string.Format((A HREF=\"/Inventory/EditInventory/{0}\"){1}(/A)", item.ID, item.Property))) 
    

    Replace the ( or ) inside the string with < or > respectively.

    0 讨论(0)
  • 2021-01-04 09:51

    I got it :) .... The issue appears to be in the way C# handles dynamic objects...Lots of users are having a blast with this...

    The fix was as simple as casting a correct type on the parameter to my extension helper... So this works:

    grid.Column(header: "", format: @<text>@Html.ActionLinkIconForEditAction("Customer", (int)item.Id)
    

    The other trick is to use "built" in "item" object and not provide your own..So, for example, this does not work:

    grid.Column(header: "", format: (customer) =>  @<text>@Html.ActionLinkIconForEditAction("Customer", (int)customer.Id)
    

    Lots of reading, tweaking, learning... Hopefully, next version will have something a lot easier to use when you need to add content to columns that are not coming directly from the model...

    Regards Z...

    0 讨论(0)
提交回复
热议问题