What is the proper MvcContrib 3 syntax for this?
-
26-02-2021 - |
Question
I inherited an ASP.NET MVC 1 website using MVCContrib 1.5 that I'm trying to upgrade to MVC 3 and MVCContrib 3.
The website had the following when using Html.Grid
.Columns(column=>{
column.For("Yes").Action(p =>{%>
<td>
<%=this.Hidden("category[" + i +"].Id").Value(p.Id)%>
<%=this.RadioButton("category[" + i + "].Interested").Value("true") %>
</td>
<%});
column.For("No").Action(p =>{%>
<td>
<%=this.RadioButton("category[" + i + "].Interested").Value("false") %>
</td>
<%});
})
In MvcContrib .For("Yes")
is no longer valid syntax to create the column with the string as the heading.
To get it to work I had to change it to .For(c=>"Yes")
and I had to add .Named("Yes")
to get the header text back. As far as I know, c=>"Yes"
doesn't actually do anything useful. What should go there? Or should I just leave it if it's not doing any harm?
Also, .Action
is deprecated and it says to use .Custom
instead. How would I convert the above?
column.Custom(p => {%>
<td style="text-align:center">
<%=this.Hidden("category[" + i + "].Id").Value(p.Id)%>
<%=this.RadioButton("category[" + i + "].Interested").Value("true") %>
</td>
<% }).Named("Yes");
This expects a return value, I'm not sure what to put and can't find many examples.
La solution
column.Custom(
@<text>
<td style="text-align:center">
@this.Hidden("category[" + i + "].Id").Value(item.Id)
@this.RadioButton("category[" + i + "].Interested").Value("true")
</td>
</text>
).Named("Yes");
Autres conseils
The For()
method is used for when you just want to show the value of a property - so you could use it like:
column.For(p => p.Id).Named("Yes");
Note you don't have to use Named()
if you've given the Id property a [Display(Name="Yes")]
attributed on the model, as this will be used by default.
You can use Custom()
when you want to display some html you've crafted, so you can just return an MvcHtmlString. With razor syntax it could look something like:
column.Custom(p => @Html.Partial("IdPartial",p.Id));