Move Processing from the View to a controller - asp.net-mvc

Currently we have a view that does some heavy processing to create an XML css feed that we send to a client. Here is a snippet of code that does some processing:
#foreach (Models.BVModels cat in ViewBag.BVSubCategories)
{
<Category>
#if (!string.IsNullOrEmpty(cat.LinkName))
{
<ExternalId>(cat.LinkName)</ExternalId>
}
else
{
<ExternalId>cat.Title</ExternalId>
}
<ParentExternalId>cat.ParentName</ParentExternalId>
<Name>cat.Title</Name>
#if (cat.Brand.ToLower() == "[hardcoded value]" && !string.IsNullOrEmpty(cat.ID))
{
if (!string.IsNullOrEmpty(cat.LinkName) && !string.IsNullOrEmpty(cat.ParentName))
{
<CategoryPageUrl><![CDATA[[URL]/gallery/#cat.ParentName.ToLower()/#cat.LinkName.ToLower()]]></CategoryPageUrl>
}
string imageName = cat.ID + ".jpg";
<ImageUrl>[URL]/imageeditor/#imageName</ImageUrl>
}
else if (cat.Brand.ToLower() == "[BRAND]")
{
if (!string.IsNullOrEmpty(cat.LinkName))
{
<CategoryPageUrl><![CDATA[[URL]/product/category/#cat.ID/#cat.LinkName.ToLower()]]></CategoryPageUrl>
}
string imagename = cat.ID + ".jpg";
<ImageUrl>[URL]/image/getdvimagebypageidandimageid/#cat.ID/1/#imagename</ImageUrl>
}
else
{
if (!string.IsNullOrEmpty(cat.LinkName))
{
<CategoryPageUrl>[URL]</CategoryPageUrl>
}
}
</Category>
}
I need to move this to a controller instead of keeping this on the view. This was done quickly to create the feed at one time, but now is way to slow. Not to mention there are a lot of hard coded values and some processing that needs to be cleaned up. Right now this cshtml page does not have a #model directive it is pulling the data from the ViewBag. I want to push this to the controller and clean up this page. Can I pass the ViewBag to the Controller and process? I am rusty on MVC and could a small shove in the right direction. So I want to call this controller ActionResult pass in the ViewBag and return an XML string that has been formatted as below.
Thanks.

I created a new controller for the feed that pulls the data from the database and return it in collections that I can iterate over and return the XML string. This replaced the long processing (20 minutes) over the ViewBag. Now I will tighten up the code and put some scrubbing in place to clean it up.
THanks.
John

Related

Multiple views modifying one object MVC

I am building a service which requires a somewhat lengthy setup process. I have it broken into 4 models and 4 corresponding views. They are Setup, Setup2, Setup3, and Setup4. Each of these views gathers information from the user which is stored in a User object. I have been passing the user along like this:
[HttpPost]
public ActionResult Setup(FormCollection values)
{
User registeringUser = new User();
registeringUser.email = User.Identity.Name;
registeringUser.fName = values["fName"];
registeringUser.lName = values["lName"];
registeringUser.phone = values["phone"];
return RedirectToAction("/Setup2", registeringUser);
}
For some reason, this seems to work just fine for the first jump (from Setup to Setup2) but after that I'm getting weird behavior, such as User. getting set to null when the User is passed to another View.
In a related, but slightly different issue, I need the last screen (Setup4) to be recursive. This screen adds a course in which the user is enrolled, and if they don't check the "This was my last class" button, it needs to basically clear the form so they can enter another course.
The entire Controller looks like this:
[HttpPost]
public ActionResult Setup4(FormCollection values, User registeringUser)
{
// values["allClassesAdded"] returns "false" as a string if box is unchecked, returns "true,false" if checked.
// Solution: parse string for "true"
if (utils.parseForTrue(values["allClassesAdded"]))
{
// TODO Redirect to "congratulations you're done" page.
database.CreateUserInDB(registeringUser);
return Redirect("/Home");
}
else
{
// Build course and add it to the list in the User
course c = new course(values);
if (Request.IsAuthenticated)
{
//registeringUser.currentCourses.Add(c);
registeringUser.AddCourse(c);
return RedirectToAction("/Setup4", registeringUser); // <---- This doesn't really work right
//return View();
}
else
{
return Redirect("/Account/Login");
}
}
}
This is my first project with MVC, so if you find that I'm doing the entire thing completely incorrectly, feel free to not answer the question I asked and offer the proper solution to this need. I'm moving an existing (pure) C# project to MVC and I'm mainly just stuck on how to work within MVC's interesting structure. I'm very grateful for any help you can give!
Thanks!
You can store user related data in session without passing it between requests
Smth like this
[HttpPost]
public ActionResult Step1(Step1Model model)
{
Session["UserRegistration"] = new UserRegistration
{
FirstName = model.fName,
....
}
....
}
[HttpPost]
public ActionResult Step2(Step2Model model)
{
var userRegistration = Session["UserRegistration"] as UserRegistration;
if (userRegistration == null) { return Redirrect("Step1"); }
userRegistration.SomeField = model.someField;
...
Session["UserRegistration"] = userRegistration;
....
}

Avoid to show Null or specific values to razor view engine

I am working on asp.net mvc3 web application using MS Sql server 2008 express rc2. In my app I have two different brands in DB and one of them have few Null or 'unknown' values (e.g. 'unknown' is added to DB instead of Null). My question is how to pass only non null values to View Engine instead of using If/Else statements in View?
in controller:
var model = _data.GetViewModel(query);
if (model != null)
{
return View(model);
}
else
return View("Error");
in ViewModel;
public int Id { get; set; }
public string Query { get; set; }
public string Brand { get; set; }
public string Family { get; set; }
public string Type { get; set; }
in Model:
public ViewModel GetViewModel(string query)
{
var data = _comp.Get(p => p.Query == query);
if (data == null) return null;
return new ViewModel
{
Id = data.id,
Brand = data.brand,
Family = data.family,
Type = data.type
};
}
in View (I am currently using If statement):
#if (Model.Brand != null)
{
<span class="brand">#Model.Brand</span>
}
#if (Model.Family != null)
{
<span class="family">#Model.Family</span>
}
#if (Model.Type != null)
{
<span class="type">#Model.Type</span>
}
Note: I want to avoid If statement because there are too many values in the Database of each brand, and many of the them are Null, So I don't want to generate Html for those Null values. I am using If/Else statement like above code, and for checking too many values in View using If, it costs Memory on server and processor, and it also slow down server response time.
I want to have an alternative method to do this. Should I use Partial views or anything else?
Please Please help me to solve this, Your help is very appreciated.
Thanks and Regards.
First, some background/context, then my suggestion.
(By the way, this all applies to any version of ASP.NET MVC or ASP.NET NancyFX (yes, there's another option out there!!), etc)
Context / Background
To solve this, people generally fall into two types of categories:
Just get data and let the View decide what to show (common one, not the proper way IMO).
The Controller should handle all the heavy lifting and give the view the exact answer (proper way, IMO).
The first way is quick and dirty. Sure it works, but it puts too much logic into the view. Views are not supposed to do any logic at all (exception: for loops, and maybe the odd if/else, maybe). The main reason for this is testing. Yep, that dirty word which people hate and think it's for hippies only. Or .. I don't have the time to test.. so I manually test, etc.. If you put any business logic into a view, you cannot test that.
The second way might seem a bit slower at first, but that's like anything - the more you practice, the faster you go. This is (IMO) the preferred method of doing things because you can test the controller. The controller should create a view model which will have -the exact- results that the view needs. Not extra. For example, imagine you want to return a list of Brands to the display/view. Most people do (the equivalent of) Get-all-brands into a list, and send that list to the view, even though 80% of those properties are -not- going to be used by that view! Even if ONE property is not going to be used by that view, do not retrieve it nor send it to the view!
So - TL;DR; do all the heavy lifting in the controller. The View is dumb. Just dump the exact view model data, to the view.
Solution to your problem
Ok, so let's roll with idea #2 and get all this stuff happening in the controller.
// Grab the results.
// ASSUMPTION: It is only returning the -exact- data I need. No more, no less.
var results = _data.GetViewModel(query);
if (model == null)
{
// Project the results into a perfectly tight & svelte view model
// 100% specific for this view.
var viewModel = results.
Select(x => new ViewModel
{
Id = x.Id,
Brand = string.IsNullOrEmpty(x.Brand)
? string.Empty
: x.Brand,
Family = string.IsNullOrEmpty(x.Family)
? string.Empty
: x.Family,
Type = string.IsNullOrEmpty(x.Type)
? string.Empty
: x.Type,
}).ToList();
return viewModel;
Testing this..
[Fact]
public void GivenSomeBrands_Index_ReturnsAViewModel()
{
// Arrange.
// NOTE: Our fake repostitory has some fake data. In it ..
// Id: 1, Brand: Gucci.
// Id: 22, Brand: null.
var controller = new BrandController(SomeFakeRepositoryThingy);
// Act.
var result = controller.Index(); // This calls that controller code, above.
// Assert.
Assert.IsNotNull(result); // Controller returned some result.
Assert.IsNotNull(result.Model); // We have some model data.
var model = result.Model as IList<ViewModel>(); // Cast the Model value.
Assert.NotNull(model); // We have a strongly typed view model.
// We check the first brand value.
Assert.Equal("Gucci", model.First().Brand);
// We know this item has a null Brand,
Assert.Equal(string.Empty, model[21].Brand); but the ViewModel converted it.
}
You could write a custom HTML helper:
public static string MyHelper<V>(this HtmlHelper helper, V value, string css)
{
if (value == null)
return "";
return String.Format("<span class='{0}'>{1}</span>", value, css);
}
Then in your view:
#Html.MyHelper(Model.Brand, "brand");
#Html.MyHelper(Model.Family, "family");
#Html.MyHelper(Model.Type, "type");

How can you use session variable to determine view?

I have my global.asax setup with the following Session_Start:
protected void Session_Start()
{
HttpContext.Current.Session.Add("sourceCode", "default");
}
On my controller I have the following:
public ActionResult Index(string sourceCode)
{
if (sourceCode != null && sourceCode != "default")
{
Session["sourceCode"] = sourceCode;
return View();
}
else
{
return View();
}
}
I want to be able to display different partial layouts based on this session variable. What is the proper way to do this? Can I load a partial view from the controller or do I need to handle that on the view?
This is a variable that I want to use site wide to determine special pricing and landing page creatives. Do I have to set this same structure up on every single controller or is there a more global way of doing this?
Thanks,
Brian
If you want to show the layout in all the pages, you might want to add the logic in the layout file. There, you will add something like that (assuming razor)
#if(HttpContext.Current.Session["someValue"]){
#*render some partial*#
}else{
#*render some other partial*#
}
By the convention of MVC, controller should decide which view it should open. For this in controller you have code like this:
public ActionResult Index(string sourceCode)
{
if (sourceCode != null && sourceCode != "default")
{
Session["sourceCode"] = sourceCode;
ViewData["PartialView"] = "partialviewname1";
}
else
{
ViewData["PartialView"] = "partialviewname2";
}
return View();
}
and in view you can write code something like this:
<div>
#Html.Partial(Convert.ToString(ViewData["PartialView"]))
</div>
and if you have decide which partial view you have to load on each and every request then you can write above logic in global action filter. Global action filter get executed before any requested action method. To know more about global action filter you can explore this link.
http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs

ASP.NET MVC: Redirect works only first time for image

I have an action method that I use to retrieve a user's picture, however some of my users might have used Facebook to log in and I don't want to return the stored picture but the one from Facebook, so I was trying something like this:
public ActionResult Picture(long id)
{
var user = UsersService.GetUser(id);
if (user.IsConnectedWithFacebook)
{
var url = "http://graph.facebook.com/" + user.GetFacebookId() + "/picture?type=square";
return Redirect(url);
}
return File(user.PictureData, user.PictureContentType);
}
and on the web page I just use:
<img src='#Url.Action("Picture", new {User.Id})' />
This works perfectly the first time I load the page, however the second time the image is not displayed. I checked calling just the action and the second time it shows a page with
"Object moved to here."
Which is why the img tag isn't loading any images.
Is there a way to prevent this from happening? I read in some other threads that people recommend to return a 301 (instead of the 302 returned by Redirect) but it returns the same page with the link (and the actual request returns a 200 instead of the 301 or 302).
Any idea on how to force it to return the redirect always?
I really don't want to add the logic to select which image to load to the page and I also don't want to request the data on the server and then return it to the page as it might be slow and become a bandwith problem.
Thanks for your help.
if (user.IsConnectedWithFacebook)
{
var url = "http://graph.facebook.com/" + user.GetFacebookId() + "/picture?type=square";
return Redirect(url);
}
I think this code should return new ContentResult() { Content = url }; instead.
Also on the UI layer, I would recommend you to supply the Id as specified in parameter of the action.
<img src='#Url.Action("Picture")' />
Rather than use redirection in your image controller, why don't you point the img src at the facebook url in your template....not sure the syntax is right (in fact, I'm sure it's not), but something like:
#if(user.IsConnectedWithFacebook)
{
<img src="http://graph.facebook.com/"......
}
else
{
<img='#Url.Action("Picture")' />
}
Alternatively, to be more elegant and reusable, move the logic into a static method on your controller:
<img src="#ProfileController.GetUserImageUrl(id) />
where
public static string GetUserImageUrl(long id)
{
var user = UsersService.GetUser(id);
if (user.IsConnectedWithFacebook)
{
return "http://graph.facebook.com/" + user.GetFacebookId() + "/picture?type=square";
}
return Url.Action("Picture");
}

Having problems with C# inside of my Razor view

I have the following code:
string init = "yes";
string html = "";
foreach (var item in v.Details) {
if (item.Substring(0, 1) != " ")
{
if (init != "yes")
{
html += "</ul>";
}
html += "<p>" + item + "</p><ul>";
}
else
{
html += "<li>" + item.Substring(1) + "</li>";
}
}
The code is in my MVC controller and it creates a string called html. The thing is I don't think it should be in the controller. I tried to put this into the view and ended up with a huge mess that doesn't seem to work. Seems I am not very good at coding C within a razor view. I just saw a lot of syntax type errors and confusion between what's C and what's HTML.
Can anyone suggest how I could make this code work within a view. Here's what I used to have:
<ul>
#foreach (var item in Model.Details)
{
<li>#item</li>
}
</ul>
This worked but as you can see I now need more processing. Would it be better to take this out of the view and if so how could I do this. I'm really hoping for a view solution but I am confused about where the put the #'s and where to put the brackets.
Any experts at coding C inside or Razor out there?
The code is in my MVC controller and it creates a string called html.
The thing is I don't think it should be in the controller
You are correct. It shouldn't be in the view neither due to the absolute mess it would create. I think this code is better suited in a custom HTML helper:
public static class HtmlExtensions
{
public static IHtmlString FormatDetails(this HtmlHelper htmlHelper, IEnumerable<string> details)
{
var init = "yes";
var html = new StringBuilder();
foreach (var item in details)
{
if (item.Substring(0, 1) != " ")
{
if (init != "yes")
{
html.Append("</ul>");
}
html.AppendFormat("<p>{0}</p><ul>", htmlHelper.Encode(item));
}
else
{
html.AppendFormat("<li>{0}</li>", htmlHelper.Encode(item.Substring(1)));
}
}
return MvcHtmlString.Create(html.ToString());
}
}
which you would invoke in your view:
#Html.FormatDetails(Model.Details)
Remark: there seems to be something wrong with the init variable. You are setting its value to yes initially but you never modify it later.

Resources