I have an ASP.NET MVC 3 Controller Action that accepts an object as follows:
//// model.ReferenceNumber equals "-1"
public ActionResult EditApplication(EditApplicationViewModel model)
{
if (!ModelState.IsValid)
{
...
}
// Existing Applicant
model.ReferenceNumber = "";
var msg = this.RenderPartialViewToString("_ExistingApplication", model);
}
When the request is received, the "model.ReferenceNumber" equals "-1". Even after setting the property on the model to empty string "", still the results of "RenderPartialViewToString" shows the value of "-1" in the "Value" property of the "input" HTML field.
Any idea why this is happening?
This is the code used to render the partial view:
http://craftycodeblog.com/2010/05/15/asp-net-mvc-render-partial-view-to-string/
public static string RenderPartialViewToString(this Controller controller, string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = controller.ControllerContext.RouteData.GetRequiredString("action");
controller.ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
Thanks
When you modify the value of some model property in your controller qction make sure you remove it from the modelstate as well:
ModelState.Remove("ReferenceNumber");
model.ReferenceNumber = "new value";
var msg = this.RenderPartialViewToString("_ExistingApplication", model);
Related
My purpose of this is to render string into view. This is what i have done:
public string RenderRazorViewToString(string viewName, object model, string controllerName)
{
ViewData.Model = model;
Type controllerType = Type.GetType(controllerName + "Controller");
object controller = Activator.CreateInstance(controllerType);
RouteData rd = new System.Web.Routing.RouteData();
rd.Values.Add("controller", "Account");
((ControllerBase)(controller)).ControllerContext = new ControllerContext(HttpContext, rd, (ControllerBase)controller);
ControllerContext cc = ((ControllerBase)(controller)).ControllerContext;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindView(cc, viewName, null);
var viewContext = new ViewContext(cc, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(cc, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
Basically what this code does is to render a view file that is found in :
var viewResult = ViewEngines.Engines.FindView(cc, viewName, null);
And renders it in :
viewResult.View.Render(viewContext, sw);
What i want is not finding view file, but i want to render view that is stored in my database as string. So what i need is:
Getting database view string
Render the database view string
Output it as string after render
How can i achieve this?
You can simply use
#Html.Raw(mystring)
mystring will be the view content stored in database
We are currently trying to create an action that returns a JsonResult and at certain times that action should also return some HTML inside it along with the other data. Is it possible to generate the HTML from another action that returns a PartialViewResult?
I think this is what you want to achieve.
Generate HTML from another action sounds strange.
You may get the same model from your repository and then render the same PartialView. You will need a method like the following.
// in a RenderingHelper class
public static string RenderViewToString(ControllerContext context, string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = context.RouteData.GetRequiredString("action");
ViewDataDictionary viewData = new ViewDataDictionary(model);
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(context, viewName);
ViewContext viewContext = new ViewContext(context, viewResult.View, viewData, new TempDataDictionary(), sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
And then you will render the partial view accordingly:
string renderedHtml = RenderingHelper.RenderViewToString(this.ControllerContext, "~/Views/MyController/MyPartial", viewModel);
Where viewModel is the model used by "other action" too and "~/Views/MyController/MyPartial"is the partial view used by "other action" too.
I am using view model to display a dropdownlist and i am also trying to get the value of the selected list, here is my view model
public class CreateJobViewModel
{
public int[] SelectedIndustriesIds { get; set; }
public IList<SelectListItem> IndustriesList { get; set; }
}
My controller
public ActionResult Create()
{
var industryList = repository.GetAllIndustries();
var model = new CreateJobViewModel
{
IndustriesList = industryList.Select(i => new SelectListItem
{
Value = i.IndustryId.ToString(),
Text = i.Name
}).ToList()
};
return View("~/Views/Dashboard/Job/Create.cshtml", model);
}
My post controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CreateJobViewModel model)
{
try
{
var job = new Job()
{
Title = "hi",
EmploymentHourId = 1,
LocationId = 1,
Salary = 50,
SalaryPeriodId = 1,
PostCode = 2131,
Role = "world",
Description = "hello",
IsPublished = false,
ShiftId = 1,
WorkDayId = 1,
NumberOfPosition = 5,
Meal = false,
SecondYearVisa = true,
Sponsorship = true,
Accommodation = true,
DurationId = 1,
IndustryExperiencePeriod = 5,
Id = User.Identity.GetUserId(),
};
foreach (int id in model.SelectedIndustriesIds)
{
var industry = repository.Industry(id);
job.Industries.Add(industry);
}
foreach (int id in model.SelectedSpecialRequirementsId)
{
var special = repository.SpecialRequirement(id);
job.SpecialRequirements.Add(special);
}
repository.AddJob(job);
return RedirectToAction("Create");
}
catch
{
return View("~/Views/Dashboard/Job/Create.cshtml");
}
}
Every time i try to submit the selected value, i get Object reference not set to an instance of an object Error on the following line in my view:
#model Taw.WebUI.Models.CreateJobViewModel
#Html.ListBoxFor(m => m.SelectedIndustriesIds, Model.IndustriesList) -- here i get the error
Any reason why?
When you submit the form your throwing an exception (confirmed in the comments) and in the catch block you are returning the view, which throws the exception you are seeing because Model.IndustriesList is null. You need to re-assign the value before you return the view.
Since you need to assign SelectLists in the GET method and in the POST method if you return the view, I tend to re-factor this to a separate method to keep the controller code a bit cleaner. Note the following code is based on your model property being public SelectList IndustriesList { get; set; } which is a bit simpler than building IList<SelectListItem>
private void ConfigureViewModel(CreateJobViewModel model)
{
var industryList = repository.GetAllIndustries();
model.IndustriesList = new SelectList(industryList, "IndustryId", "Name")
// any other common stuff
}
and then in the action methods
public ActionResult Create()
{
var model = new CreateJobViewModel();
ConfigureViewModel(model);
return View(model);
}
public ActionResult Create(CreateJobViewModel model)
{
try
{
....
}
catch
{
ConfigureViewModel(model);
return View(model);
}
}
Note its also good practice to test if the model is valid before attempting to save it
public ActionResult Create(CreateJobViewModel model)
{
if (!ModelState.IsValid)
{
ConfigureViewModel(model);
return View(model); // return the view so the user can correct validation errors
}
....
This question already has answers here:
How do you create a dropdownlist from an enum in ASP.NET MVC?
(36 answers)
Closed 8 years ago.
I've been finding all over the place that the common way to bind Enums to DropDowns is through helper methods, which seems a bit overbearing for such a seemingly simple task.
What is the best way to bind Enums to DropDownLists in ASP.Net MVC 4?
You can to this:
#Html.DropDownListFor(model => model.Type, Enum.GetNames(typeof(Rewards.Models.PropertyType)).Select(e => new SelectListItem { Text = e }))
I think it is about the only (clean) way, which is a pity, but at least there are a few options out there. I'd recommend having a look at this blog: http://paulthecyclist.com/2013/05/24/enum-dropdown/
Sorry, it's too long to copy here, but the gist is that he created a new HTML helper method for this.
All the source code is available on GitHub.
Enums are supported by the framework since MVC 5.1:
#Html.EnumDropDownListFor(m => m.Palette)
Displayed text can be customized:
public enum Palette
{
[Display(Name = "Black & White")]
BlackAndWhite,
Colour
}
MSDN link: http://www.asp.net/mvc/overview/releases/mvc51-release-notes#Enum
In my Controller:
var feedTypeList = new Dictionary<short, string>();
foreach (var item in Enum.GetValues(typeof(FeedType)))
{
feedTypeList.Add((short)item, Enum.GetName(typeof(FeedType), item));
}
ViewBag.FeedTypeList = new SelectList(feedTypeList, "Key", "Value", feed.FeedType);
In my View:
#Html.DropDownList("FeedType", (SelectList)ViewBag.FeedTypeList)
The solution from PaulTheCyclist is spot on. But I wouldn't use RESX (I'd have to add a new .resx file for each new enum??)
Here is my HtmlHelper Expression:
public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TEnum>> expression, object attributes = null)
{
//Get metadata from enum
var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var enumType = GetNonNullableModelType(metadata);
var values = Enum.GetValues(enumType).Cast<TEnum>();
//Convert enumeration items into SelectListItems
var items =
from value in values
select new SelectListItem
{
Text = value.ToDescription(),
Value = value.ToString(),
Selected = value.Equals(metadata.Model)
};
//Check for nullable value types
if (metadata.IsNullableValueType)
{
var emptyItem = new List<SelectListItem>
{
new SelectListItem {Text = string.Empty, Value = string.Empty}
};
items = emptyItem.Concat(items);
}
//Return the regular DropDownlist helper
return htmlHelper.DropDownListFor(expression, items, attributes);
}
Here is how I declare my enums:
[Flags]
public enum LoanApplicationType
{
[Description("Undefined")]
Undefined = 0,
[Description("Personal Loan")]
PersonalLoan = 1,
[Description("Mortgage Loan")]
MortgageLoan = 2,
[Description("Vehicle Loan")]
VehicleLoan = 4,
[Description("Small Business")]
SmallBusiness = 8,
}
And here is the call from a Razor View:
<div class="control-group span2">
<div class="controls">
#Html.EnumDropDownListFor(m => m.LoanType, new { #class = "span2" })
</div>
</div>
Where #Model.LoanType is an model property of the LoanApplicationType type
UPDATE: Sorry, forgot to include code for the helper function ToDescription()
/// <summary>
/// Returns Description Attribute information for an Enum value
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ToDescription(this Enum value)
{
if (value == null)
{
return string.Empty;
}
var attributes = (DescriptionAttribute[]) value.GetType().GetField(
Convert.ToString(value)).GetCustomAttributes(typeof (DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : Convert.ToString(value);
}
Technically, you don't need a helper method, since Html.DropdownListFor only requires a SelectList or Ienumerable<SelectListItem>. You can just turn your enums into such an output and feed it in that way.
I use a static library method to convert enums into List<SelectListItem> with a few params/options:
public static List<SelectListItem> GetEnumsByType<T>(bool useFriendlyName = false, List<T> exclude = null,
List<T> eachSelected = null, bool useIntValue = true) where T : struct, IConvertible
{
var enumList = from enumItem in EnumUtil.GetEnumValuesFor<T>()
where (exclude == null || !exclude.Contains(enumItem))
select enumItem;
var list = new List<SelectListItem>();
foreach (var item in enumList)
{
var selItem = new SelectListItem();
selItem.Text = (useFriendlyName) ? item.ToFriendlyString() : item.ToString();
selItem.Value = (useIntValue) ? item.To<int>().ToString() : item.ToString();
if (eachSelected != null && eachSelected.Contains(item))
selItem.Selected = true;
list.Add(selItem);
}
return list;
}
public static class EnumUtil
{
public static IEnumerable<T> GetEnumValuesFor<T>()
{
return Enum.GetValues(typeof(T)).Cast<T>();
}
// other stuff in here too...
}
/// <summary>
/// Turns Camelcase or underscore separated phrases into properly spaces phrases
/// "DogWithMustard".ToFriendlyString() == "Dog With Mustard"
/// </summary>
public static string ToFriendlyString(this object o)
{
var s = o.ToString();
s = s.Replace("__", " / ").Replace("_", " ");
char[] origArray = s.ToCharArray();
List<char> newCharList = new List<char>();
for (int i = 0; i < origArray.Count(); i++)
{
if (origArray[i].ToString() == origArray[i].ToString().ToUpper())
{
newCharList.Add(' ');
}
newCharList.Add(origArray[i]);
}
s = new string(newCharList.ToArray()).TrimStart();
return s;
}
Your ViewModel can pass in the options you want. Here's a fairly complex one:
public IEnumerable<SelectListItem> PaymentMethodChoices
{
get
{
var exclusions = new List<Membership.Payment.PaymentMethod> { Membership.Payment.PaymentMethod.Unknown, Membership.Payment.PaymentMethod.Reversal };
var selected = new List<Membership.Payment.PaymentMethod> { this.SelectedPaymentMethod };
return GetEnumsByType<Membership.Payment.PaymentMethod>(useFriendlyName: true, exclude: exclusions, eachSelected: selected);
}
}
So you wire your View's DropDownList against that IEnumerable<SelectListItem> property.
Extending the html helper to do it works well, but if you'd like to be able to change the text of the drop down values based on DisplayAttribute mappings, then you would need to modify it similar to this,
(Do this pre MVC 5.1, it's included in 5.1+)
public static IHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> html, Expression<Func<TModel, TEnum>> expression)
{
var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
var enumType = Nullable.GetUnderlyingType(metadata.ModelType) ?? metadata.ModelType;
var enumValues = Enum.GetValues(enumType).Cast<object>();
var items = enumValues.Select(item =>
{
var type = item.GetType();
var member = type.GetMember(item.ToString());
var attribute = member[0].GetCustomAttribute<DisplayAttribute>();
string text = attribute != null ? ((DisplayAttribute)attribute).Name : item.ToString();
string value = ((int)item).ToString();
bool selected = item.Equals(metadata.Model);
return new SelectListItem
{
Text = text,
Value = value,
Selected = selected
};
});
return html.DropDownListFor(expression, items, string.Empty, null);
}
I am trying to make a simple multi-language website. Overriding FindView to get the file based on the language is easy, my problem comes in the routing part.
I would like to do is: (have a website with 2 languages - pt-br and en-us, let abbreviate to onlu pt and en - the default would be PT)
User types www.mysite.com -> would find the languages in the user header request and if the user doesn´t have any of them, he would be redirected to the default PT, so the end result would be www.mysite.com in Portuguese. If he has en, he would be redirected to www.mysite.com/en/ , the result would be the view in English.
If the user types www.mysite.com/pt/ -> I would check that he wants the default and would redirect to www.mysite.com in Portuguese.
I did a custom engine to get the correct view based on the language. But it´s not working 100% because the routing problem.
IN my case I am trying to route with
routes.MapRoute(
"Localization", // Route name
"{lang}/{controller}/{action}", // URL with parameters
new { lang = "pt", controller = "Home", action = "Index" } //defaults
);
But with this, when someone types /pt/ will not redirect to the root of the site.
Another problem with the route is that I don´t want to anyone typing the controller name, I just want the action, my website just have one Home controller with few actions.
Another problem is the that I want a different action name, like, CONTACT in English and CONTATO in Portuguese, they should appear on the address bar like www.mysite.com/Contato or www.mysite.com/en/Contact
Mu Engine I did base on this solution http://www.counity.at/blog/2012/asp-net-mvc3-localization-using-culture-dependent-views/ by Guido Breitenhuber
Here is the code with some tweaks (it´s not working 100% yet)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
namespace Ala.MultiLanguage
{
public class LocalizedViewLocation : RazorViewEngine
{
private static readonly string[] _emptyLocations = new string[0];
public string[] LocalizedViewLocationFormats { get; set; }
public string[] LocalizedMasterLocationFormats { get; set; }
protected string[] LocalizedPartialViewLocationFormats { get; set; }
public LocalizedViewLocation()
{
// Define the localized view locations
// 0: Language
// 1: View name
// 2: Controller name
LocalizedViewLocationFormats = new[] {
"~/Views/{0}/{2}/{1}.cshtml",
"~/Views/Shared/{0}/{1}.cshtml"};
MasterLocationFormats = new[] {
"~/Views/{0}/{2}/{1}.cshtml",
"~/Views/Shared/{0}/{1}.cshtml"};
LocalizedPartialViewLocationFormats = new[] {
"~/Views/{0}/{2}/{1}.cshtml",
"~/Views/Shared/{0}/{1}.cshtml"};
}
public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
{
if (controllerContext == null)
throw new ArgumentNullException("controllerContext");
if (String.IsNullOrEmpty(partialViewName))
throw new ArgumentException("Parameter partialViewName is null or empty.", "partialViewName");
string[] searched;
var controllerName = controllerContext.RouteData.GetRequiredString("controller");
var partialPath = GetPath(controllerContext, LocalizedPartialViewLocationFormats, partialViewName, controllerName, out searched);
if (String.IsNullOrEmpty(partialPath))
{
var baseRes = base.FindPartialView(controllerContext, partialViewName, useCache);
if (baseRes.View != null)
return baseRes;
return new ViewEngineResult(searched.Union(baseRes.SearchedLocations));
}
return new ViewEngineResult(CreatePartialView(controllerContext, partialPath), this);
}
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
if (controllerContext == null)
throw new ArgumentNullException("controllerContext");
if (String.IsNullOrEmpty(viewName))
throw new ArgumentException("Parameter viewName is null or empty.", "viewName");
string[] viewLocationsSearched;
string[] masterLocationsSearched;
var controllerName = controllerContext.RouteData.GetRequiredString("controller");
var viewPath = GetPath(controllerContext, LocalizedViewLocationFormats, viewName, controllerName, out viewLocationsSearched);
var masterPath = GetPath(controllerContext, LocalizedMasterLocationFormats, masterName, controllerName, out masterLocationsSearched);
if (String.IsNullOrEmpty(viewPath) || (String.IsNullOrEmpty(masterPath) && !String.IsNullOrEmpty(masterName)))
{
var baseRes = base.FindView(controllerContext, viewName, masterName, useCache);
if (baseRes.View != null)
return baseRes;
return new ViewEngineResult(viewLocationsSearched.Union(masterLocationsSearched).Union(baseRes.SearchedLocations));
}
return new ViewEngineResult(CreateView(controllerContext, viewPath, masterPath), this);
}
private string GetPath(ControllerContext controllerContext, string[] locations, string name, string controllerName, out string[] searchedLocations)
{
searchedLocations = _emptyLocations;
if (String.IsNullOrEmpty(name))
return String.Empty;
if (IsSpecificPath(name))
return String.Empty;
return GetPathFromGeneralName(controllerContext, locations, name, controllerName, ref searchedLocations);
}
private static bool IsSpecificPath(string name)
{
char c = name[0];
return (c == '~' || c == '/');
}
private string GetPathFromGeneralName(ControllerContext controllerContext, string[] locations, string name, string controllerName, ref string[] searchedLocations)
{
var result = String.Empty;
searchedLocations = new string[locations.Length];
for (int i = 0; i < locations.Length; i++)
{
var location = locations[i];
var virtualPath = string.Format(CultureInfo.InvariantCulture, location, CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, name, controllerName);
if (FileExists(controllerContext, virtualPath))
{
searchedLocations = _emptyLocations;
result = virtualPath;
break;
}
searchedLocations[i] = virtualPath;
}
return result;
}
}
}
Well, after some timing trying to do in the best way, I ´ve found one solution much easier and fast to implement.
Just mapped the routes that I want one-by-one in the global.asax. This will work and will be fast to implement if you have only few pages and languages.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Default-BR", "", new { controller = "Home", action = "Index" });
routes.MapRoute("Default-EN", "en", new { controller = "Home", action = "IndexEN" });
routes.MapRoute("HotelBR", "Hotel", new { controller = "Home", action = "Index" });
routes.MapRoute("HotelEN", "en/Hotel", new { controller = "Home", action = "IndexEN" });
routes.MapRoute("Apartamento", "Apartamento", new { controller = "Home", action = "Apartamentos" });
routes.MapRoute("Apartamentos", "Apartamentos", new { controller = "Home", action = "Apartamentos" });
routes.MapRoute("Apartments", "en/Apartments", new { controller = "Home", action = "ApartamentosEN" });
routes.MapRoute("Localizacao", "Localizacao", new { controller = "Home", action = "Localizacao" });
routes.MapRoute("Location", "en/Location", new { controller = "Home", action = "LocalizacaoEN" });
routes.MapRoute("Tarifa", "Tarifa", new { controller = "Home", action = "Tarifas" });
routes.MapRoute("Tarifas", "Tarifas", new { controller = "Home", action = "Tarifas" });
routes.MapRoute("Rates", "en/Rates", new { controller = "Home", action = "TarifasEN" });
routes.MapRoute("Reserva", "Reserva", new { controller = "Home", action = "Reservas" });
routes.MapRoute("Reservas", "Reservas", new { controller = "Home", action = "Reservas" });
routes.MapRoute("Booking", "en/Booking", new { controller = "Home", action = "ReservasEN" });
routes.MapRoute("Contato", "Contato", new { controller = "Home", action = "Contato" });
routes.MapRoute("Contact", "en/Contact", new { controller = "Home", action = "ContatoEN" });