Unexpected HTML from Razor - asp.net-mvc-3

I have some Razor code in a view that is supposed to route through to a different part of my model:
#Html.ActionLink("Edit", "Edit", "Journal", new { id = item.JOURNAL.REF_ID })
but when I look at the HTML that is emmitted, it is does not relect what I have written:
Edit
How can I stop this from happening?

That's because you are using the wrong overload. It should be like this:
#Html.ActionLink("Edit", "Edit", "Journal", new { id = item.JOURNAL.REF_ID }, null)
Let's see why you are using the wrong overload. Let's break down what you wrote:
#Html.ActionLink(
"Edit", // linkText
"Edit", // actionName
"Journal", // routeValues
new { id = item.JOURNAL.REF_ID } // htmlAttributes
)
See the problem?
And now let's break down the correct way:
#Html.ActionLink(
"Edit", // linkText
"Edit", // actionName
"Journal", // controllerName
new { id = item.JOURNAL.REF_ID }, // routeValues
null // htmlAttributes
)
See the difference?
I would recommend you reading very carefully the documentation and the different available overloads of the ActionLink helper as well as the exact significance of their parameters.

Related

How do you set the linktext on url in controller

is there a way to create an action link in my controller and set the link text? Can't find an overload...
Url.Action("Edit", "Controller", new { id = myId });
To create a link in controller use Helpers like this:
string link = HtmlHelper.GenerateLink(this.ControllerContext.RequestContext, System.Web.Routing.RouteTable.Routes, "My link", "Root", "About", "Home", null, null);
Here you go: ActionLink Method
#Html.ActionLink("Edit", "Edit", "Controller", new { id = myId })

pass data-icon attributes in razor html helper

I want to following html code using asp.net mvc 3 razor html helper:
<input type="text" .... . placeholder="Login" data-icon="user" />
I have try this one:
#Html.TextBoxFor(m => m.UserName, new { placeholder = "Login", data-icon = "user" })
or
#Html.TextBoxFor(m => m.UserName, new { placeholder = "Login", #data-icon = "user" })
Displayed Error:
Invalid anonymous type members declaration.
This might due to dash in data-icon not taken as attributes. How could I add data-icon attributes in text box field.
try this
#Html.TextBoxFor(m => m.UserName, new { placeholder = "Login", data_icon = "user" })
Yes, you can't write like that but you can write your own Extension to solve this problem. Here is the sample code:
public static MvcHtmlString MyInput(this HtmlHelper htmlHelper, string name, string value, string icon)
{
var attrs = new Dictionary<string,object>();
attrs.Add("data-icon", icon);
return htmlHelper.TextBox(name, name, value, attrs);
}
Or you can also use in razor like this:
#{
var attrs = new Dictionary<string, object>();
attrs.Add("placeholder","Login");
attrs.Add("data-icon","user");
}
#Html.TextBoxFor(m => m.UserName, attrs)
Plz don't forget to mark it's right answer if it helps you :-)
This is really the same as vNext's second alternative, but if you prefer to write it in-line:
#Html.TextBoxFor(m => m.UserName, new Dictionary<string, object> { { "placeholder", "Login" }, { "data-icon", "user" } })

Html.ActionLink MVC3 ASPX / v. ?=

when I use the following actionlink:
<%: Html.ActionLink("Study Cases", "Index", "CourseCases", new { id = Model.ID }, new { #class = "t-button", #style = "width:240px; color:white; text-align:center" })%>
The url address in the browser is:
http://localhost:11111/CourseCases/Index/9
How can I change it so the url will be
http://localhost:11111/CourseCases?courseId=9
It works when I use:
return RedirectToAction("Index", "CourseCases", new { courseId = id });
in the controller.
Thanks in adance.
Like this:
<%= Html.ActionLink(
"Study Cases",
"Index",
"CourseCases",
new { courseId = Model.ID },
new {
#class = "t-button",
#style = "width:240px; color:white; text-align:center"
}
) %>
The reason why your code generates http://localhost:11111/CourseCases/Index/9 is because the {id} is used by the default route that was generated when you created your ASP.NET MVC 3 application, so when you specify id = Model.ID it will match the route pattern definition in your Global.asax which is {controller}/{action}/{id} thus you get CourseCases/Index/9.

Create RouteValueDictionary on server and use in aspx?

I am tring to pass a RouteValueDictionary to my aspx so that I can use it as the parameters for an Ajax.BeginForm method. I load it up like so:
RouteValues = new System.Web.Routing.RouteValueDictionary();
RouteValues.Add("FindingId", thisFinding.Id);
RouteValues.Add("ReportId", thisFinding.ReportSection.ReportId);
and then add it to my model without issue. When I put it as the parameter to the BeginForm method it renders the action as this:
/SolidWaste/Finding/LoadSection?Count=3&Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D
Here is the aspx code:
(Ajax.BeginForm(Model.FormModel.Action,
Model.FormModel.Controller,
Model.FormModel.RouteValues,
new AjaxOptions {
HttpMethod = "Post",
InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
UpdateTargetId = "WindowContent",
}, new { id = FormId })) { %>
<input name="submit" type="submit" class="button" value="" style="float: right;"/>
<% } //End Form %>
Here is the View Model that represents Model.FormModel
public class FormViewModel {
public string Action { get; set; }
public string Controller { get; set; }
public string Method { get; set; }
public RouteValueDictionary RouteValues { get; set; }
}
Any idea why it is not serializing the RouteValueDictionary into the proper URL on the action? I would like to use an object here rather than build the RouteValues by hand with new { field = vale }
Ah, you are using the wrong overload. It's normal. The ASP.NET MVC team really made a mess out of this API. You gotta be careful which method you are invoking. Here's the overload that you need:
<% using (Ajax.BeginForm(
Model.FormModel.Action, // actionName
Model.FormModel.Controller, // controllerName
Model.FormModel.RouteValues, // routeValues
new AjaxOptions { // ajaxOptions
HttpMethod = "Post",
InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
UpdateTargetId = "WindowContent",
},
new Dictionary<string, object> { { "id", FormId } }) // htmlAttributes
) { %>
<input name="submit" type="submit" class="button" value="" style="float: right;"/>
<% } %>
Notice the correct overload? You were using the one that was taking routeValues and htmlAttributes as anonymous objects, except that you was passing Model.FormModel.RouteValues as a RouteValueDictionary which basically crapped your overload.
Hit F12 while hovering the cursor over the BeginForm and if you are lucky enough and Intellisense works fine for you in Razor views (which rarely happens) you will get redirected to the method you are actually invoking and realize your mistake.

How can I add an anchor tag to my URL?

MVC 3.net I want to add an anchor to the end a url.
I tried to include an anchor query string but the hash '#' changes to %23 or something like that in the url.
Is there a way of working around this?
There is an overload of the ActionLink helper that allows you to specify the fragment:
#Html.ActionLink(
"Link Text", // linkText
"Action", // actionName
"Controller", // controllerName
null, // protocol
null, // hostName
"fragment", // fragment
new { id = "123" }, // routeValues
null // htmlAttributes
)
will produce (assuming default routes):
Link Text
UPDATE:
and if you wanted to do this within a controller action performing a redirect you could use the GenerateUrl method:
public ActionResult Index()
{
var url = UrlHelper.GenerateUrl(
null,
"Action",
"Controller",
null,
null,
"fragment",
new RouteValueDictionary(new { id = "123" }),
Url.RouteCollection,
Url.RequestContext,
false
);
return Redirect(url);
}

Resources