I am converting my application in razor template,i used the Html.CheckBoxlist in one of my views.
Please suggest me how can I achieve this in razor
Here is my code:
#Html.CheckBoxlist("abc", Model.CheckBoxListItems)
Razor is simply a view engine it has nothing to do with the methods you call.
If you have such an html helper method, it should work with #Html.CheckBoxlist(parameters)
Related
I am using MVC and razor for a style Guide. I need to display the code in the view so others can use it on the site.
I need to display the code without the Razor helper executing the function.
I am trying to do this via a Razor helper. I want this helper to render the button as well as display the exact MVC Razor helper code in the view (rather than have it display the outputted HTML from the helper).
#helper ShowCode() {
<div>
#Html.TextBox("textBox")
</div>
}
Then I call the helper below and try and display the code:
// render the button
#ShowCode()
// display the button code
<pre>
<code>
#ShowCode().ToString()
</code>
</pre>
However, this displays the following:
I need it to display the following code instead of the HTML:
<div>
#Html.TextBox("textBox")
</div>
How can I display the code within the Razor helper without it being outputted as html? (I want to display exactly what is in the Razor helper). Is this possible?
You can't actually do it like that. Razor returns HTML there is no way to get actual Razor template code as string.
Is it possible to insert html block in #page Razor page in the new ASP.NET core?
I want to insert html block the way I'm doing it in Angular - like when you insert components. Here's the problem: only in the Layout page the IntelliSense allows me to use #RenderSection
Also, the #RenderPage is not recognizable in the IntelliSense.
If I'm using the #RenderPage it also gives error
So is it possible / is there a way to use the #RenderPage or to insert html blocks within .cshtml files with Razor?
Thanks
If you need to render another view into a view, the method that you need is
#Html.Partial("partialViewName").
This allow you to send a Model and other parameters.
Use the #Html.Partial() method. You can provide it with a Partial View and Model for that view.
Example:
#Html.Partial("_NavBar")
or
#Html.Partial("_NavBar, Model.Customer")
Is it possible to render Razor in Html.Raw()? I have a dynamic page being generated that uses the Html.Raw() method to render the page that is created in the controller. My raw html has razor tags in it. That is, I am trying to generate a link by
#Html.Raw(Model.myRawHtmlContainingRazorTags);
where Model.myRawHtmlContainingRazorTags contains
<html>
...
...
#Model.TheLink
...
...
<html>
I need the value of Model.TheLinkto be rendered when #Html.Raw(Model.myRawHtmlContainingRazorTags) is called within the cshtml.
You may have some architectural issues that lead you to have some razor code on your Model property.
Anyway, you can do it by using some external libs such RazorTemplates.
Here is a sample :
var template = Template.Compile(Model.myRawHtmlContainingRazorTags);
#Html.Raw(template.Render(Model));
Is there anyway to allow asp tags in chtml MVC pages? Do I have to declare something in the view itself? I know its possible to have an aspx page in the solution, but I would like to combine controls in the chtml page.
Example:
#using (Html.BeginForm())
{
<asp:calander>...
etc
Actually Razor does not support WebForm controls, but you need to see Scott Hanselman's example if you really want to mix n match
http://www.hanselman.com/blog/PlugInHybridsASPNETWebFormsAndASPMVCAndASPNETDynamicDataSideBySide.aspx
I'm trying to build a custom control in ASP.NET MVC. The thing that I want to avoid is mixing the view (html) and the code (C#) in these controls.
After hours of googling and struggling with this problem I've finally thought of creating helper methods that use the ASP view Engine thought the ViewPage class.
I'm able to create an instance of the class and even load an ascx template using LoadControl and LoadTemplate methods but the problem is that when I pass a ViewData object to an instance of ViewPage class I cannot see the ViewData in the ViewPage and I'm getting this error:
The ViewUserControl '~/Components/Controls/EditableLabel/Views/EditableLabel.ascx' cannot find an IViewDataContainer. The ViewUserControl must be inside a ViewPage, ViewMasterPage, or another ViewUserControl.
Generally the final effect that I want to achieve is loading controls with some parameters, these parameters should be readable in the template (ascx).
example:
<% Html.EditableLabel(Writer, "memberName", "Name", "Antonio", "/identity/updateProperty", "memberName"); %>
Is there a way to pass the ViewData to the ViewPage instance that I create?
Is there a smarter way to create custom controls/widgets in ASP.NET MVC?
Thanks in advance
You can pass VD to the VP from controller via "return View(...)" or via "ViewData[name] = value".
It's better to use ascx controls which are inherited from ViewUserControl in order to create a custom control. In this way you easily can pass data to it:
<% Html.RenderPartial("FooControl", new FooViewData(){ Name="aa", Type="awesome", Id = 2}); %>
<% Html.RenderPartial("FooControl", new FooViewData(){ Name="aa", Type="awesome", Id = 2}, ViewData); %>
And all necessary parameters you can define in your FooViewData class.
If you really want to write an extension to the HtmlHelper which will load some ascx controls you should look at a Repeater class in MvcFutures (sources of MVC are here). But I'm sure that in a common case you'll not need this.
I am tracking with zihotki on this one. With #2 I will add a caveat, however. In the default implementation, which uses neutered ASPX and ASCX as views, creating MVC controls (use the template to ensure your dog is neutered) is the best option.
Now for the caveat. ASPX and ASCX do not have to be the views. If you look at Phil Haack's blog, you will see some other view engines mentioned, like the Spark View Engine (ASP.NET MVC Northwind Demo Using the Spark View Engine). In fact, Phil has set up a sample of using alternative view engines in this entry: Rendering A Single View Using Multiple ViewEngines.
I am not suggesting that you move to other view engines, only that there is an option, if you find one that better fits your problem domain. Sometimes it is better to stick with the out of the box implementation. In this case, keep things simple with ASCX controls.
By the way, if you are seriously getting into MVC as your UI story, you should look at the codeplex projects for MVC. The most important here is probably MVC.Contrib, but there are a few other gems in the list.