ActionUrl in ASP.NET MVC Preview 5 - asp.net-mvc

I don't need a Link but rather only the href= part of the ActionLink.
But if I call Html.ActionLink(...) I get a back.
Is there a way to just return the URL of the Action while not getting the ?

MVC also provides a UrlHelper class which can do the same thing:
<%=Url.Action(actionName)%>
<%=Url.Action(actionName, htmlValues)%>
<%=Url.Action(actionName, controllerName, htmlValues)%>

Edit: in response to commment, now including parameters:
<% =Html.BuildUrlFromExpression<YourController>(c => c.YourAction(parameter)) %>

Related

Html.ActionLink URL error

return RedirecttoAction("Success")
how to generate redirection?
You are using the wrong overload of Html.ActionLink, you need
<%: Html.ActionLink("linkText", "actionName", "controllerName") %>
Without the third controllerName parameter it will default to the current controller which appears to be in this case your UserController when I expect you want to direct to the AccountController. That is why the ActionLink works in your other view.

How to avoid default URL encoding in ASP.NET MVC Html Helpers like RouteLink

I want my url like this:
"http://domain.com/tag/高兴"
My route mapping:
routes.MapRoute("Tag", "tag/{name}", new { controller = "Tag", action="Index" });
But Html.RouteLink will encode the parameters as default. If I use Html.RouteLink in my View, the generated html is:
高兴
Is there any way to avoid this?
Changed my example.
This works in my case
<%= HttpUtility.UrlDecode(Html.RouteLink("Test", new { id = "高兴" }).ToString())%>
Make sure to change from <%: to <%=

Asp.Net Mvc 2 - RenderAction List with a create

First, I use Asp.Net MVC 2 RC 2.
What I want to do is to list a comment view and below this view being able to add comment (with validations). For example, something like when you add comment in stackoverflow. Except that my page should work with or without javascript enabled.
So to solve that problem I use the new RenderAction and it partially solved my problem. I got my list view that calls my addcomment usercontrol with RenderAction.
The validations work. My problem occurs when I try to add a comment that it's valid. The page didn't refresh correctly. If I got in the database, my comment is added, but it's doesn't have been refresh in my list view and the add comment form isn't clear.
I think it's because of how the workflow is rendered.
Maybe if someone got a example or blog about this, it's can help me to get it right...
At the bottom of my Comment/List.aspx
<% Html.RenderAction("Create", "Comment"); %>
In Comment/Create.ascx
<% using (Html.BeginForm(
ViewContext.ParentActionViewContext.RouteData
.Values["action"].ToString(),
ViewContext.ParentActionViewContext.RouteData
.Values["controller"].ToString(),
FormMethod.Post, new { id = "createForm" })){ %>
You can force the parent View to refresh itself with a small hack involving ViewContext.ParentActionViewContext.
In your CommentController class:
public ActionResult Create(Comment comment)
{
...
if (isValid) // Comment entered in form is valid
{
ControllerContext.ParentActionViewContext.ViewData["SuccessfullCreate"] = true;
}
...
}
And in your Comment/List.aspx page (view) :
<% Html.RenderAction("Create", "Comment"); %>
<%
if (ViewContext.ViewData["SuccessfulCreate"] != null)
{
string action = ViewContext.RouteData.Values["action"].ToString();
string controller = ViewContext.RouteData.Values["controller"].ToString();
string url = "/" + controller + "/" + action;
Response.Redirect(url);
}
%>
So basically, what's happening is that the child action is "telling" the parent action to refresh itself by using the parent's ViewData.
It's kind of a hack, but it works fine for what's you're doing.

MVC: Model is null in a view's aspx

I'm following this MVC tutorial and when I add a View for the Edit action, Model is null in the following snippet on the .aspx page:
<%= Html.TextBox("Id", Model.Id) %>
I'm learning MVC, so please understand if I'm doing a dumb thing. But as far as I can see, I've following the steps in the tutorial pretty well. And actually added the Create action and it works correctly.
Ideas appreciated.
Is your view strongly typed?
<%# Page Language="C#" MasterPageFile="~/Views/Shared/TwoColumnUI.Master" Inherits="System.Web.Mvc.ViewPage<MyObject>" %>
then you would need to pass in an object of type MyObject from your controller action method
return View(new MyObject() { Id = 42 } );
Did you set the model in the controller? What does your controller method look like? Are you just returning View()? You need to pass the model as a parameter to that call like they do in the example:
return View(movieToEdit);

Dynamic RadEditor Creation through HtmlHelper

I am using the Telerik RadEditor (Q1 2009 SP1) in our ASP.NET MVC (RTM) project. The editor works great when rendered as a hardcoded object on the page with a static id. But when extending with an HtmlHelper to do dynamic creation by passing in an Id it seems to render the html as all lowercase for the tag. Does the HtmlHelper object mess with this innately by chance? The attributes look upper and lowercase respectively but this seems strange. Here is my code....thanks in advance!
<% if (placeholder.Type.ToLower() == "richtext") { %>
<%= Html.RadEditor("placeholder_" + placeholder.Name) %>
<% } else { %>
<%= Html.TextBox("placeholder_" + placeholder.Name, null, new { #class = placeholder.Type }) %>
<% } %>
The helper looks like this....
public static string RadEditor(this HtmlHelper html, string Id)
{
var sb = new StringBuilder();
sb.Append("<telerik:RadEditor ID='" + Id + "' Runat='server' DialogHandlerUrl='~/Telerik.Web.UI.DialogHandler.axd'>");
sb.Append("<Content>");
sb.Append("</Content>");
sb.Append("</telerik:RadEditor>");
return sb.ToString();
}
For the time being you cannot render RadEditor without having a valid Page object with a ScriptManager. We (Telerik that is) plan to add support for "standalone" rendering in the near future. Should be announced in a blog post so stay tuned.
The problem is the tag is a server side control. When you place it hardcoded in your page, the server side tag gets translated to html. When you're using the htmlhelper, you're outputting the html and it doesn't get processed as a server side tag.
If you want to do something dynamic, you should use a UserControl (.ascx file) and then use the Html.RenderPartial method.

Resources