Relationship between Razor and aspx
Sorry, I think the error in Razor is not caused by Razor engine.
There have been several similiar questions but I am still confused. Take the following code as example:
<% Html.RenderAction("partial"); %>
@{Html.RenderAction("partial");}
the aspx page runs well but Razor throw an error: "No route in the route table matches the supplied values." And even this is wrong, too.
Html.RenderAction("partial");
So why? What's the "@" really mean? What's the difference or relationshi开发者_Python百科p between aspx and cshtml?
Well lets start off with what the @ symbol means, this is used to tell the view that you are about to use razor view engine syntax, for example ...
@Html.something
Also you can use c# within a block, for example ...
@{
string something = "value";
//you can later use that variable anywhere in the page
}
When rendering a partial view in mvc using razor it will look like this
@Html.Partial("Folder Name/Partial View Name")
there is no need to end razor code with a ;
Razor is very smart and can tell when you are breaking back into html
The folder in which the partial view is in under the folder Views. You can always put the partial view in the shared folder and you wont have to specify a folder name, it will check there by default I'm pretty sure.
Think of a partial view as a user control. You can create partial views that just have links in it, or you can create a strongly typed partial view that will need to have a model sent to the containing view to render the partial or you will get an error.
I hope this helps if you have any more questions let me know.
Introducing Razor should be able to answer your question on what is "@"
and Razor.
The @ symbol tells the razor view engine that you are going to be inserting some C# code at that point. You can do that two ways
Inline
@Html.TextBox("LastName", Model.LastName)
notice that there is no ;, it is not needed
Code block
@{
ViewBag.FirstName = "Bob";
}
all of the code in a code block is stanard C# and each statement must end with a ";".
Mix of HTML and Razor
<div class="label">
@Html.LabelFor(model => model.FirstName)
</div>
the razor parser can figure out where the C# and HTML code begins and ends.
精彩评论