Problem with ASP.NET MVC Edtior Templates - asp.net-mvc

I am trying to create an editor template for column in a Telerik MVC Grid. After clicking edit this simple string column should show the same string value and next to it I would like to show a button or image with an onclick event.
I CAN'T DO THIS!
I have found some really simple examples. Let's forget about the button or image for now and just display the same damn string, based on instructions found in this article:
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.Encode(ViewData.TemplateInfo.FormattedModelValue) %>
and:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= ViewData.TemplateInfo.FormattedModelValue %>
or:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Model %>
None of these work. In fact, when I step into this server side, ViewData.TemplateInfo.FormattedModelValue and Model don't have a value. What's going on? What is it in the grid that is preventing this? More complex Editor Templates like comboboxes persist the data back to the editor template.
How can I simply display the same text in an editor template with some further options like buttons that I will later change the data?
Steve

Steve, can you provide a sample of your controller code? It sounds as though it's not setting the model data correctly.

Related

How to show user control in MVC view

I have my user control in a class library and I have a public method in it which returns the user control as html string. I want to display this HTML in my MVC view. I am using the following code in my view:
<div>
<% MyControlsNamespace.MyControl mvc = new MyControl();
mvc.LoadMyControl(); %>
</div>
LoadMyControl() returns html as string. I can't see any thing in my view when I open it in a browser. I am new to MVC and I knew I am missing some thing here. Any guess??? I am using MVC1
Use <% Html.RenderPartial("Path/To.ascx") %> or <%= Html.Partial("Path/To.ascx") %>.
Use RenderPartial when you're fine with the user-control rendering directly to the response stream, or use Partial if you want to intercept the output as an MvcHtmlString. RenderPartial is faster and generally preferred, only use Partial if you know you need the MvcHtmlString.
More information is available here: Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction

Telerik not showing in intellisense of VS 2008

I was coding an aspx page to use the telerik grid. But telerik is not showing in intellisense of VS 2008.
Following are the things done so far:
1. Added the telerik dll in the reference folder.
Code part
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage"%>
<%# Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%# Import Namespace="Telerik.Web.Mvc" %>
Index
<% foreach(var v in (List<String>)ViewData["names"])
{
Response.Write(v);
%>
<br />
<%
}
%>
<% Html. %>
Thanks
The Visual Studio Intellisense in views is far from perfect. Personally what I do is to add the assembly (<assemblies> tag) and the namespace (<namespaces> tag) in web.config, so that it is available in all views. This way I no longer need to add any Import section in each view. After doing this you probably might need to reopen the view for the changes to take effect. Even restart Visual Studio. If it works at runtime and you don't get Intellisense at compile-time, well, I wish they improve it in future versions.

Passing parameters to ASCX user control in MVC

I am trying to pass a string parameter to an ASCX. I want to set this to the text property of a label. In the code below it shows betwen the div tags (ignore the % signs in the html tags).
<# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" >
<%asp:Label ID="Label2" runat="server" **Text="<%# Model %> Test"** CssClass="isslabel"><%/asp:Label>
<%div><%: Model %><%/div>
However in the label tag no matter what I put between the angle brackets (bit it bold) in the text tag I cannot get the parameter to appear. I have tried <%: Model %> to no avail. Is the issue that the code block is inside quotes and am I just missing some character?
Why are you trying to use a web forms user control with MVC?
Assuming the model is the string you want to display and you are passing this through correctly, along the lines of
<% Html.RenderPartial("MyUserControlView", "My String To Display"); %>
In your "parent" page, you will be able to do the following in your ascx:
<%= Html.Label(Model) %>
Instead of <asp:label...
Update
If you need to specify then you have a number of options, you could wrap the Html.Label call in a div and specify the class of the div (updating your css accordingly), you could use a display template, or simply explicitly use the Html like the following:
<label for="someIdThatICouldUseAnotherHtmlExtensionMethodToGet"><%: Model %></label>
The key problem with your code (as now also pointed out in the comments by #mare) is that you are trying to use a web forms control in an MVC view.

Asp.Net MVC - RenderPartial - Create in a List view

I got a page that lists all my articles (Articles/List.aspx).
I also got a control that create article (Article/Create.ascx).
I will like that my List.aspx page that's render the Create.ascx to be able to create article.
I know that in MVC, the preferred approach is one page by action. But in this case I need to do that. It's a design issue and how the client want the Web site to work.
So for now, I got the following code in List.aspx :
<% Html.RenderPartial("Create", new Domain.Models.Article()); %>
That render correctly. But when I hit the create button, it's doesn't go in the Create[post] method of my ArticleController.
Any idea why and how I could resolve that issue ?
If you have problems with the button, it's not going to have anything to do with how you're rendering the user control. We need to see the form markup that the button is inside, that will show what the problem is most likely.
But just for reference, you're probably looking to do something like this:
<% using (Html.BeginForm("Create",
ViewContext.RouteData.Values["Controller"].ToString())) { %>
your control markup here
<% } %>

Problems with Site.Master and ASP.NET MVC

i'm trying to make my site master page (views/shared/site.master) strongly typed.
eg. Inherits="TestProject.Mvc.Views.Shared.Site"
I can't seem to get this work. Once i make the site.master page strongly, typed, Visual Studio seems to 'loose' what <%= Html.XXX %> is. Also, the page throws an error when i try to display the default index route.
The SiteMasterViewData class exists in the views/shared/ folder and has been included at the top of the master page via..
<%# Import Namespace="TestProject.Mvc.Views.Shared"%>
Can this be done? is there a better way to do this?
Damn - found my own answer.
All masterpages in the ASP.NET MVC v1. need to inherit from:
<%# Master
Language="C#"
Inherits="System.Web.Mvc.ViewMasterPage" %>
so if u want to strongly type it, you can do this.
<%# Master
Language="C#"
Inherits="System.Web.Mvc.ViewMasterPage<SiteMasterViewData>" %>
HTH's other peeps :)

Resources