ViewData as a hyperlink - asp.net-mvc

I an ASP.NET MVC we can pass some data via a ViewData and then show it on a page:
<%: ViewData["Foo"]%>
But how to make a hyperlink out of it?
Something like following:
<%: Html.ActionLink(ViewData["Foo"], "Index", "Home") %>

Cast it to string:
Html.ActionLink((string)ViewData["Foo"], "Index", "Home")
In general, however, try to avoid using ViewData and use a strongly typed ViewModel instead. (Thus, you would have avoided the problem in this question, btw).

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.

What is the right way to '#include file' in MVC?

I would like to do something like this:
<!--#include file="../stuff/foo/box.aspx"-->
But doing this in an ASP.Net MVC application it just feels wrong. Is there a better way of achieving the same thing in a ASP.Net MVC project ?
<%: Html.Partial("~/Views/foo/box.ascx") %>
or:
<% Html.RenderPartial("~/Views/foo/box.ascx"); %>
or the best of them all use an editor template (if this partial contains inputs for editing the view model property):
<%: Html.EditorFor(x => x.MyModelProperty) %>
or a display template (if this partial contains only display of view model property):
<%: Html.DisplayFor(x => x.MyModelProperty) %>
and their Razor equivalence
#Html.Partial("~/Views/foo/box.ascx")
#{Html.RenderPartial("~/Views/foo/box.ascx");}
#Html.EditorFor(x => x.MyModelProperty)
#Html.DisplayFor(x => x.MyModelProperty)
You should make a partial view.
You can use
Html.RenderPartial('~/Views/Login/Box.ascx');
RenderPartial allows to render part of the page using the same context. If you want to render using new context, use
Html.RenderAction("Box","Login"); //Box - Action, Login - Controller

RenderPartial control in same view folder

I have a file in my view folder Users called UserViewControl.cshtml.
My code in the actual view (Users.cshtml) is:
#Html.RenderPartial("RegisterViewControl")
Error:The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments
I do not want to type the path fully like this as the whole view folders might move in the future:
#Html.RenderPartial("~/Views/Users/RegisterViewControl.cshtml")
Code in RegisterViewControl.cshtml:
#model SampleMVC.Web.ViewModels.RegisterModel
#using (Html.BeginForm("Register", "Auth", FormMethod.Post, new { Id = "ERForm" }))
{
#Html.TextBoxFor(model => model.Name)
#Html.TextBoxFor(model => model.Email)
#Html.PasswordFor(model => model.Password)
}
This is a form that will be submitted by ajax, but I want all the validation from the viewmodel.
It should be like this:
#{Html.RenderPartial("RegisterViewControl");}
And that's because the RenderPartial extension method doesn't return anything. It writes directly to the output. In aspx you use it like this:
<% Html.RenderPartial("RegisterViewControl"); %>
instead of:
<%= Html.RenderPartial("RegisterViewControl") %>
So the same rules apply for razor.
You could alternatively use
#Html.Partial("RegisterViewControl")
I had this issue as well and got this directly from Scott Guthrie's blog post:
using #Html.RenderPartial() from a Razor view doesnt work.
Rather than call Html.RenderPartial() use just
#Html.Partial("partialname")
That returns a string and will work.
Alternatively, if you really want to use the void return method you
can use this syntax:
#{Html.RenderPartial("partialName")}
But #Html.Partial() is the cleanest.
The link for this is: http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

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);

Asp.Net MVC Call another controller from view

Let say I'm on the page "Home/Index" and I want to go to the page MyOtherController/Index/1
How can I do this ?
I try :
<%= Html.ActionLink("Test", "Index", "MyOtherController", new { id=item.Id }) %>
Did I also have to add a route in Global.aspx file ?
One option is to specify the name of the controller in the list of routevalues:
<%= Html.ActionLink("Test", "Index"
, new { controller = "MyOtherController", id = item.Id }) %>
An alternative is to use the overload of ActionLink with htmlAttributes = null:
<%= Html.ActionLink("Test", "Index"
, "MyOtherController", new { id = item.Id }, null) %>
The default route in the ASP.NET MVC template takes care of the routing in this case.
I don't believe ActionLink has an overload matching that particular signature. You would need to add "null" after your route values to find a matching one (for htmlAttributes). Ole's solution would be cleaner though so it's really a matter of preference. It also will help with readability so you don't have to guess whether each parameter is link text, an action/controller, etc.

Resources