Html.ActionLink MVC3 ASPX / v. ?= - asp.net-mvc-3

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.

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" } })

Unexpected HTML from Razor

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.

MVC 3 routing problem

In my site i have the following routing table:
routes.MapRoute("Something",
"sites/{company}/{site}/",
new { controller = "Home", action = "LoadClientSite" },
new[] { "CorrectOnLine.Controllers" });
routes.MapRoute("Default1", // Route name
"{company}/{site}/", // URL with parameters
new { controller = "Home", action = "DefaultRedirect" },
new[] { "CorrectOnLine.Controllers" }
);
routes.MapRoute("Default2", // Route name
"{company}/{site}/{id}", // URL with parameters
new { controller = "Home", action = "DefaultRedirect" },
new[] { "CorrectOnLine.Controllers" }
);
I am using the following link to call an action:
#(Html.Action("index", "home", new { area = "ClientsSites", CompanyID = Model.CompanyID, SiteID = Model.SiteID, CategoryID = Model.CategoryID, Message = Model.Message }))
In the view i have an image:
<img alt="" src="../../../../../WareHouse/Sites/logo_site_#(Model.Site.SiteID).jpg" height="250px" width="750px" />
The problem is that when the image loades MVC tries to locate the image src in the routing table , conntroller = WareHouse, action = Sites.
How can i make MVC to only load the iamge and not try to load it as a view?
Thanks,
Alex
You could ignore route for all paths that end in the .jpg extension...
routes.IgnoreRoute("{*alljpeg}", new {alljpeg=#".*\.jpg(/.*)?"});

MVC3 Routing problem HTTP 404

I'm at a loss trying to figure out why I have Actions that are returning 404 'The Resource cannot be found' errors.
Controller Name: ItemManagementController
My Index view has list of items in a table. Each row contains two links, 'Delete' and 'Request Update'. The Delete link calls a Delete action and works fine. The Request Update gives me the 404 error, and seems as if trying to navigate to a URL like http://localhost/TVAPDev/ItemManagement/RequestUpdate?itemID=9.
I have to assume I'm missing something simple, as they are identical in what they do from the view aspect. The actions as defined in the controller are both similar except that they call different methods on a service layer, but that's it.
Here are my two Controller Actions.
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult Delete(int itemID) {
var svc = new ItemManagementService(_repository);
var requestModel = svc.GetItemDeleteModel(itemID);
svc.DeleteItem(requestModel);
var message = requestModel.ActionMessage;
return Json(new { id = itemID, ChangeStatus = requestModel.ItemDetails.ItemChangeStatus.ToString(), ChangeType = requestModel.ItemDetails.ItemChangeType.ToString(), message});
}
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult RequestUpdate(int itemID) {
var svc = new ItemManagementService(_repository);
var requestModel = svc.GetItemUpdateRequestModel(itemID);
svc.RequestItemUpdate(requestModel);
var message = requestModel.ActionMessage;
return Json(new { id = itemID, ChangeStatus = requestModel.ItemDetails.ItemChangeStatus.ToString(), ChangeType = requestModel.ItemDetails.ItemChangeType.ToString(), message });
}
Here are the links as they are defined in the View
<td class="tblist" style="white-space: nowrap;">
#Html.ActionLink("Request Update", "RequestUpdate", new { itemID = item.ItemID }, new AjaxOptions {
HttpMethod = "POST",
Confirm = "Request an Update to this item?",
OnSuccess = "actionCompleted"
})break;
}
</td>
<td class="tblist" style="white-space: nowrap;">
#Ajax.ActionLink("Delete", "Delete", new { itemID = item.ItemID }, new AjaxOptions {
HttpMethod = "POST",
Confirm = "Are you sure you want to delete this Item?",
OnSuccess = "actionCompleted"
})
</td>
Again, the Delete here works without issue. The Request Update link gives me the Http 404 error.
Anyhelp here would be greatly appreciated.
Why are you using AjaxOptions on a normal Html.ActionLink (which is what Request Update is)?
Maybe you wanted it to be like this:
#Ajax.ActionLink(
"Request Update",
"RequestUpdate",
new {
itemID = item.ItemID
},
new AjaxOptions {
HttpMethod = "POST",
Confirm = "Request an Update to this item?",
OnSuccess = "actionCompleted"
}
)
Check your View code... the delete is using the Ajax html helper and the update is using the regular html helper.

Resources