HTML markup rendering issue in ViewData? Also, MultiView functionality in MVC? - c#

i'm trying to populate a ViewData instance with html markup like shown below. When the page renders, the html tags get rendered as text and not html markup. Anyone know why?
Controller code:
if (user.ActivationStatus == false)
{
...
ViewData["Message"] = "<p>Your account has been activated, you're good to go.</p>";
}
else
{
ViewData["Message"] = "<p>Sorry, this account has already been activated.</p>";
}
return View();
View code:
<h2>Confirmation</h2>
<p><%: ViewData["Message"] %></p>
Secondly, I have used the MultiView feature in asp.net webforms in the past. This functionality is ideal and like to implement a similar functionality in MVC.
Is there any way i can call different PartialViews dependant on the function outcome(like the 'if' statement above) from controller code to a placeholder in the View?

<%: %> == Html.Encode(ViewData["Message"]);is HTML Encoding your string...
Try this:
<%= ViewData["Message"]%>
Also, you should be able to control Partial Controls using ViewData and depending on the values in ViewData you could render different partials. You could also strongly type your view to a model that represents the behavior your after on the page...

You can create an MvcHtmlString
<%: MvcHtmlString.Create(ViewData["Message"]) %>
But really you should not have the html in your controller.
By making your view strongly-typed you could instead do something like this.
<h2>Confirmation</h2>
<% if(Model.ActivationStatus) { %>
<p>Sorry, this account has already been activated.</p>
<% } else { %>
<p>Your account has been activated, you're good to go.</p>
<% } %>

Change your view output to:
<h2>Confirmation</h2>
<p><%= ViewData["Message"] %></p>
Only use <%: %> syntax when you want output to be automatically HTML encoded.

Related

What is the PROPER way to create an ASPX View in MVC?

Essentially, my issue is this: I'm trying to display a ViewData variable on an ASPX page, but I'm getting "ViewData does not exist in the current context."
What led me here? I'm new to MVC, and am following an online tutorial. In Lesson 1, he has us create a view by going to Add > View, which creates a Razor view. Then, in the next tutorial, he's teaching us to use ViewData to pass a variable to the view. But, when he displays it, he's doing so using the <%: %> method, which doesn't work on my Razor page. So, I try to create an ASPX page in my View folder by creating a Web Form the traditional way. When I do that, I get the error mentioned above. I have successfully figured out how to get the Razor method working in my cshtml page, but what is the appropriate method should I want to do it the "angle bracket - percent" way?
Edit: Basically, all I did was follow the instructions for Tutorial 2 in the following walk-through. He doesn't really explain HOW he created the ASPX page (or that he used an ASPX page at all). If anyone knows of a better tutorial that's more complete, I'm all ears.
http://www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in#Lab1:- Creating a simple hello world ASP.NET MVC Application
Edit2: By request, here's my code:
ASPX:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="MVCLearn.Views.FirstMVC.Index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%= ViewData["vwTest"] %>
</div>
</form>
</body>
</html>
And here is my Controller:
public class FirstMVCController : Controller
{
// GET: FirstMVC
public ActionResult Index()
{
ViewData["vwtest"] = "Something";
return View();
}
public ActionResult SayHello()
{
return View("HelloView");
}
}
<%= ViewData["vwTest"] %>
ViewData["vwtest"] = "Something";
vwTest != vwtest
You should write same name
<%= ViewData["vwTest"] %>
ViewData["vwTest"] = "Something";

How to view HTML file in Partial View of ASP.NET MVC (not Razor)

I have an application in which I have created a partial view as below:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
and my parent view has the following code:
<div>
<% #Html.RenderPartial("ViewerControl"); %>
</div>
Now, I want to open an HTML file in the partial view. I am not sure how to do it. Quick sample code will be highly appreciated.
Views do not support server side include directives or similar. Your best bet would be to create an action result that returns the markup as a ContentResult.
public ContentResult HtmlFile() {
return Content(File.ReadAllText(Server.MapPath("Give the path here")));
}
Then in your view:
<%: Html.Raw(Html.Action("HtmlFile")) %>
Totally off the cuff, but you get the point: invoke a server side action to retrieve your markup, or alternatively deliver it via the Model on the previous Action Result execution.

Access view information from master page

I have a master page and would like to print out the view it is currently being used to render. For instance, someone types in /path/method into the browser. In my master page, I'd like to to print <%= "path" %> somewhere and <%= "method" %> somewhere.
How can I get access to this information from the master page?
At the controller of the view you are accessing:
ViewBag.ViewName = "MyViewName" or ViewData["ViewName"] = "MyViewName"
Then use it at the MasterPage
<%= ViewBag.ViewName %> or <%= ViewData["ViewName"] %>
This is more like what I was looking for:
IView view = ((ViewPage)this.Page).ViewContext.View;
string viewname = ((WebFormView)view).ViewPath;

How to check the existance of a value from asp designer page?

I'm getting a list of string from db like:
AddCustomer,
AddUser,
ListCustomer,
ListUser
These are the prefix of asp pages.
I need to hide and show certain pages in the page. Following is the html snippet:
<li>Customer Management
<ul>
<%if (AddCustomer) //how to check whether my string is present or not, is it possible?
{ %><li>Add Customer</li><%} %>
<li>List Customer</li>
</ul>
</li>
Have you tried writing method then calling it from aspx page?
<%# YourMethodName((string)Eval("AddCustomer")) %>

MVC keeping the PartialView in its own context - ignore the main view holding the partial view

I'm looking at the partialview components of the MVC Framework.
i want my partial view to be handled in its own action and for the rest of the view to handle itself, but i'm getting an exception because the main page is not getting its view fired.
Am i going around this the wrong way?
My Main View (Jobs/Index.aspx):
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication3.Models.JobViewModel>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% Html.RenderPartial("JobListing", Model.Jobs); %>
</asp:Content>
The partialview (Jobs/JobListing.ascx):
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<List<MvcApplication3.Models.Job>>" %>
<table>
<tr>
<td> Job Title </td>
<td> Job Location</td>
</tr>
<%
foreach (var job in Model)
{
%>
<tr>
<td>
<%= job.Title %>
</td>
<td>
<%= job.Location %>
</td>
</tr>
<%
}
%>
<% Html.BeginForm("DoSomeStuff", "Job", null, FormMethod.Post); %>
<%= Html.TextBox("SomeInfo") %>
<button type="submit" id="submit" />
<% Html.EndForm(); %>
The main controller for both the main view (Index) and the partialview (DoSomeStuff())
public class JobController : Controller
{
public ActionResult Index()
{
JobProvider provider = new JobProvider(Session);
JobViewModel vm = new JobViewModel();
vm.Jobs = provider.GetJobs();
return View(vm);
}
public PartialViewResult DoSomeStuff()
{
return PartialView("JobListing");
}
}
As you can see in the partial view, it has its own form that posts to the Action called DoSomeStuff(). i want this action to handle any data submitted from that form.
but when the form is submitted the main action (Index) does not fire and then i get an exception as the Model (.Models.JobViewModel) is not passed to the view that the partialview (JobListings) lives in.
basically what im saying is, if i have a myview.aspx with lots of html.RenderPartialView('apartialview') that have forms in them, can i get it so these forms
post to their own actions and the main view (with what ever model it inherits) is handled as well. Rather then having all the form submitting code in the main action for the view.
am i do this wrong?
I would follow POST-REDIRECT-GET pattern and would define DoSomeStuff like:
public class JobController : Controller
{
[HttpPost]
public RedirectToRouteResult DoSomeStuff(DoSomeStuffModel model)
{
//Do some stuff with model
return RedirectToAction("Index");
}
}
If you don't want to reload whole form, you can use jQuery Form Plugin. If you use it, you can stay with PartialViewResult. After posting partial view form, if you specify target option, content of div containing partial view code will be replaced with returned html.
Is the DoSomeStuff() action being called?
If so it is not surprising it isn't working. RenderPartial is to call a partial page directly without having a controller action. But perhaps because one exists it is calling that method. However, that method doesn't receive Jobs and is also not passing a Model back to the view, so it makes sense that it can't get the view model.
Really, you should be using RenderPartial if you have no need for an action to execute. If you do, you should use Html.RenderAction or Html.Action

Categories

Resources