I have a MVC project where I am displaying currency amounts in different formats based on the currency of the object.
My project handles the ##.00 format just find, but I have some objects that are using Euros for example that need to be in the ##,00 format instead of ##.00.
Then the page loads in read only mode both formats display correctly, but then the form is edited, it initially shows the currencies correctly, but if I click out of a Euro formatted amount, it drops the "," out of the amount (for example 1000,00 changes to 100000).
In the model, the fields are of DataType = Currency and are decimals. Also, if 1000,00 is tried to save, it is returned as invalid.
How to I get the textbox to handle the both currency formats?
This is my code
#model Projections.Web.ViewModels.Projections.ProjectionFormView
#Html.HiddenFor(model => model.Number)
#Html.HiddenFor(model => model.Department)
#* -- Quarters -- *#
<div class="edit-fields">
<div class="control-group">
#Html.LabelFor(model => model.Q12017, new {#class = "control-label"})
<div class="controls">
#Html.EditorFor(model => model.Q12017)
</div>
</div>
<div class="control-group">
#Html.LabelFor(model => model.Q22017, new {#class = "control-label"})
<div class="controls">
#Html.EditorFor(model => model.Q22017)
</div>
</div>
<div class="control-group">
#Html.LabelFor(model => model.Q32017, new {#class = "control-label"})
<div class="controls">
#Html.EditorFor(model => model.Q32017)
</div>
</div>
<div class="control-group">
#Html.LabelFor(model => model.Q42017, new { #class = "control-label" })
<div class="controls">
#Html.EditorFor(model => model.Q42017)
</div>
</div>
<div class="control-group">
#Html.LabelFor(model => model.Q12018, new { #class = "control-label" })
<div class="controls">
#Html.EditorFor(model => model.Q12018)
</div>
</div>
</div>
Controller:
public ActionResult Edit(string number, string department)
{
// Get the project we're trying to edit a projection for.
var projects = _db.Projects.FindBy(
x => x.Number == number &&
x.PMUsername == SessionWrapper.CurrentManager,
null,
"Projection").ToList();
if (!projects.Any())
{
return Error(
Notices.ProjectNotFoundTitle,
string.Format(Notices.ProjectNotFound, number)
);
}
var project = projects.SingleOrDefault(x => x.Department == department);
var baseProject = projects.SingleOrDefault(x => x.Department == string.Empty);
// Project doesn't exist, error time!
if (project == null || baseProject == null)
{
return Error(
Notices.DepartmentNotFoundTitle,
string.Format(Notices.DepartmentNotFound, department, number)
);
}
project.Projection = project.Projection ?? new Projection { Number = number, Department = department, Project = project };
SetProjectCulture(project.ProjectCurrencyCode);
var projection = Mapper.Map<ProjectionFormView>(project.Projection);
projection.BaseProjectName = baseProject.Name;
return View(projection);
}
private void SetProjectCulture(string currencyCode)
{
var uiHelper = DependencyResolver.Current.GetService<IThreadUIHelper>();
if (!uiHelper.SetUICulture(currencyCode)) return;
var notice = new Notification(string.Format(Notices.ProjectCurrencyNotice, currencyCode));
NotificationHandler.AddNotification(notice);
}
Model
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace Projections.Web.ViewModels.Projections
{
public class Forecast : INotifyPropertyChanged
{
private decimal _q12017;
private decimal _q22017;
private decimal _q32017;
private decimal _q42017;
private decimal _q12018;
[DataType(DataType.Currency)]
public decimal Q12017
{
get { return _q12017; }
set { _q12017 = value; ForecastChanged("Q12017"); }
}
[DataType(DataType.Currency)]
public decimal Q22017
{
get { return _q22017; }
set { _q22017 = value; ForecastChanged("Q22017"); }
}
[DataType(DataType.Currency)]
public decimal Q32017
{
get { return _q32017; }
set { _q32017 = value; ForecastChanged("Q32017"); }
}
[DataType(DataType.Currency)]
public decimal Q42017
{
get { return _q42017; }
set { _q42017 = value; ForecastChanged("Q42017"); }
}
[DataType(DataType.Currency)]
public decimal Q12018
{
get { return _q12018; }
set { _q12018 = value; ForecastChanged("Q12018"); }
}
public decimal Total
{
get
{
var quarters = GetType().GetProperties().Where(x => x.Name.StartsWith("Q")).ToList();
return quarters.Sum(q => (decimal?)q.GetValue(this, null) ?? default(decimal));
}
}
public DateTime? Modified { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void ForecastChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public decimal? this[string name]
{
get
{
var property = GetType().GetProperty(name);
if (property == null) throw new InvalidOperationException("Invalid property specified.");
if (property.PropertyType == typeof(decimal))
{
return property.GetValue(this, null) as decimal?;
}
throw new InvalidOperationException("Invalid property specified.");
}
}
}
}
The default model binder will only handle decimals with a U.S.-style decimal point (.). If you need to accept a comma, you need a custom model binder. I use the following, which I found original here.
public class DecimalModelBinder : IModelBinder {
public object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext) {
ValueProviderResult valueResult = bindingContext.ValueProvider
.GetValue(bindingContext.ModelName);
ModelState modelState = new ModelState { Value = valueResult };
object actualValue = null;
try {
actualValue = Convert.ToDecimal(valueResult.AttemptedValue,
CultureInfo.CurrentCulture);
}
catch (FormatException e) {
modelState.Errors.Add(e);
}
bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
return actualValue;
}
}
Then, register it in Application_Start in Global.asax:
ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
You may need to alter the model binder to your specific usage scenario. This version simply converts based on the current culture, but you may need to do some different type of processing here if you're going to be handling both periods and commas as "decimal points".
Related
I have implemented culture in my system. The change with selected combox works fine.
[
[
Now I want depending on User Language to change the language.
I read other descriptions but I can't get it to work.
======================= My switch class Model ===================
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
namespace WorkCollaboration.Models
{
public class SwitchCultureModel
{
public CultureInfo CurrentUICulture { get; set; }
public List<CultureInfo> SupportedCultures { get; set; }
}
}
I have a general logon service where I check user privileges languages....
In the system I created a page were I want to change User language session and so on.
This is how my page looks like
[
========= This is my razor page ==========
#page
#using Microsoft.AspNetCore.Localization
#using Microsoft.AspNetCore.Mvc.Localization
#model WorkCollaboration.Pages.LogonService.LanguageModel
#{
ViewData["Title"] = "UserLanguage";
}
#inject IViewLocalizer Localizer
<h1>#Localizer["Edit"]</h1>
<h4>#Localizer["LogonService"]</h4>
<p>
<a asp-page="/LogonService/IndexLanguage">#Localizer["Back to Index"]</a>
</p>
<hr />
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="LogonService.WorkColUserMail" />
<input type="hidden" asp-for="LogonService.WorkColUserId" class="form-control" />
<input type="hidden" asp-for="LogonService.WorkColUserPassword" class="form-control" />
<input type="hidden" asp-for="LogonService.WorkColUserPasswordConfirm" class="form-control" />
<input type="hidden" asp-for="LogonService.WorkColUserName" class="form-control" />
<input type="hidden" asp-for="LogonService.WorkColUserVerificationMode" class="form-control" />
<input type="hidden" asp-for="LogonService.WorkColEnteredPassword" class="form-control" />
<input type="hidden" asp-for="LogonService.WorkColLoggedIn" class="form-control" />
<div class="form-group">
#Html.LabelFor(model => model.LogonService.WorkColUserLanguage, htmlAttributes: new { #class = "form-group" })
<div class="form-group">
#Html.DropDownListFor(model => model.LogonService.WorkColUserLanguage, new List<SelectListItem>
{
new SelectListItem {Text = "DE", Value = "DE", Selected = true },
new SelectListItem {Text = "EN", Value = "EN" },
}, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.LogonService.WorkColUserLanguage, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-page="/LogonService/IndexLanguage">#Localizer["Back to List"]</a>
</div>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
In c# I call the following threds
if (LogonService.WorkColUserLanguage == "DE")
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("de");
Thread.CurrentThread.CurrentUICulture = new
CultureInfo("de");
}
if (LogonService.WorkColUserLanguage == "EN")
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("en");
Thread.CurrentThread.CurrentUICulture = new
CultureInfo("en");
}
However the page language continues on same language
==========Here my c# code ======================
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualBasic;
using WorkCollaboration.Data;
using WorkCollaboration.Models;
using System.Collections;
using System.Web;
namespace WorkCollaboration.Pages.LogonService
{
public class LanguageModel : PageModel
{
private readonly WorkCollaboration.Data.WorkCollaborationContext _context;
public LanguageModel(WorkCollaboration.Data.WorkCollaborationContext context)
{
_context = context;
}
[BindProperty]
public Models.LogonService LogonService { get; set; }
public async Task<IActionResult> OnGetAsync(string id)
{
if (id == null)
{
return NotFound();
}
LogonService = await _context.LogonService.FirstOrDefaultAsync(m => m.WorkColUserMail == id);
if (LogonService == null)
{
return NotFound();
}
return Page();
}
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://aka.ms/RazorPagesCRUD.
public async Task<IActionResult> OnPostAsync()
{
string id = LogonService.WorkColUserMail;
byte[] ep = LogonService.WorkColEnteredPassword;
string ln = LogonService.WorkColUserLanguage;
string SessionKeyName = "_Name";
string SessionKeyId = "_Id";
string SessionKeyDate = "_Date";
string SessionKeyLang = "_Lang";
if (id == null)
{
return NotFound();
}
//====================================
// Load WorColUser Data with Mail Key
//====================================
LogonService = await _context.LogonService.FirstOrDefaultAsync(m => m.WorkColUserMail == id);
//====================================
// Overwrite Data with GUI Data
//====================================
LogonService.WorkColEnteredPassword = ep;
LogonService.WorkColUserLanguage = ln;
LogonService.WorkColLoggedIn = true;
//====================================
// Create SessionID
//====================================
DateTime now = DateTime.Now;
string valueSystemKeyDates = now.ToString("yyyymmddhh:mm:ss");
HttpContext.Session.SetString(SessionKeyDate, valueSystemKeyDates);
HttpContext.Session.SetString(SessionKeyId, Convert.ToString(LogonService.WorkColUserId));
HttpContext.Session.SetString(SessionKeyName, LogonService.WorkColUserMail);
HttpContext.Session.SetString(SessionKeyLang, LogonService.WorkColUserLanguage);
var SessionIdDate = HttpContext.Session.GetString(SessionKeyDate);
var SessionIdId = HttpContext.Session.GetString(SessionKeyId);
var SessionIdName = HttpContext.Session.GetString(SessionKeyName);
var SessionIdLang = HttpContext.Session.GetString(SessionKeyLang);
LogonService.WorkColUserSessionId = Strings.RTrim(SessionIdDate) + " " + Strings.RTrim(SessionIdName) + " " + Strings.RTrim(SessionIdId) + " " + Strings.RTrim(SessionIdLang);
//=========================================
// Change Culture on specific user language
//=========================================
if (LogonService.WorkColUserLanguage == "DE")
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("de");
Thread.CurrentThread.CurrentUICulture = new
CultureInfo("de");
}
if (LogonService.WorkColUserLanguage == "EN")
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("en");
Thread.CurrentThread.CurrentUICulture = new
CultureInfo("en");
}
if (!ModelState.IsValid)
{
return Page();
}
_context.Attach(LogonService).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!LogonServiceExists(LogonService.WorkColUserMail))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToPage("/LogonService/IndexLanguage");
}
private bool LogonServiceExists(string id)
{
return _context.LogonService.Any(e => e.WorkColUserMail == id);
}
}
}
Tks for helping
==============================================================================
Update: 25.1.2021: I investigated further why my culture defined values do not work. And I found the reason why. In my SwithCulturViewComponent.cs The culture is on every page load or exited refreshed to the one defined in the languages in the combo (see Pic 1 or 2)
Now I tried to influence the switch class by setting the culture info from the user language from the session.
=================== SwitchCultureViewComponet ===============
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using WorkCollaboration.Models;
namespace WorkCollaboration.ViewComponents
{
public class SwitchCultureViewComponent : ViewComponent
{
private readonly IOptions<RequestLocalizationOptions> localizationOptions;
public SwitchCultureViewComponent(IOptions<RequestLocalizationOptions> localizationOptions)
{
this.localizationOptions = localizationOptions;
}
public IViewComponentResult Invoke()
{
//=============================================
// Get Session Language
//=============================================
string SessionKeyName = "_Name";
string SessionKeyId = "_Id";
string SessionKeyDate = "_Date";
string SessionKeyLang = "_Lang";
var SessionIdDate = HttpContext.Session.GetString(SessionKeyDate);
var SessionIdId = HttpContext.Session.GetString(SessionKeyId);
var SessionIdName = HttpContext.Session.GetString(SessionKeyName);
var SessionIdLang = HttpContext.Session.GetString(SessionKeyLang);
if (SessionIdId == null)
{
SessionIdId = "0";
}
if (SessionIdId == "")
{
SessionIdId = "0";
}
if (SessionIdLang == null)
{
SessionIdLang = "DE";
}
if (SessionIdLang == "")
{
SessionIdLang = "DE";
}
//=========================================
// Change Culture on specific user language
//=========================================
if (SessionIdLang == "DE")
{
CultureInfo myCIintl = new CultureInfo("de-CH", false);
CultureInfo current = CultureInfo.CurrentCulture;
Console.WriteLine("The current culture is {0}", current.Name);
CultureInfo newCulture;
newCulture = new CultureInfo("de-CH");
CultureInfo.CurrentCulture = newCulture;
Console.WriteLine("The current culture is now {0}",
CultureInfo.CurrentCulture.Name);
CultureInfo.CurrentUICulture = new CultureInfo("de-CH", false);
// CultureInfo.CurrentUICulture = CultureInfo.CreateSpecificCulture("de-CH");
Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name);
}
if (SessionIdLang == "DE")
{
CultureInfo myCIintl = new CultureInfo("en-US", false);
CultureInfo current = CultureInfo.CurrentCulture;
Console.WriteLine("The current culture is {0}", current.Name);
CultureInfo newCulture;
newCulture = new CultureInfo("en-US");
CultureInfo.CurrentCulture = newCulture;
Console.WriteLine("The current culture is now {0}",
CultureInfo.CurrentCulture.Name);
CultureInfo.CurrentUICulture = new CultureInfo("en-US", false);
// CultureInfo.CurrentUICulture = CultureInfo.CreateSpecificCulture("de-CH");
Console.WriteLine("CurrentUICulture is now {0}.", CultureInfo.CurrentUICulture.Name);
}
// How can I influence this code below. I only want this to be executed when I change the UI language code in the dropdown
// Otherwise it should use the custure Info from the above user languages
var cultureFeature = HttpContext.Features.Get<IRequestCultureFeature>();
var model = new SwitchCultureModel
{
SupportedCultures = localizationOptions.Value.SupportedUICultures.ToList(),
CurrentUICulture = cultureFeature.RequestCulture.UICulture
};
return View(model);
}
}
}
How can I influence this code below? I only want this to be executed when I change the UI language code in the dropdown
Otherwise it should use the culture Info from the above user languages
Whenever I try to CurrentUICuture within the "var"...
============ Section in the SwitchViewComponent.cs ==============
var model = new SwitchCultureModel
{
SupportedCultures =
localizationOptions.Value.SupportedUICultures.ToList(),
CurrentUICulture = cultureFeature.RequestCulture.UICulture
};
return View(model);
I get errors. Thank you for your support
How would I generate a select list, where the text field, is made up of two or more text columns, eg: Where I have a Description and Rate field in my database, I want to combine these to show:
Large--£200
Medium--£150
Small--£100
Controller code is:
var stands = db.Stands.Where(s => s.ExhibitorID == null).ToList();
ViewBag.StandID = new SelectList(stands,"StandID", "Description" + "-- £" + "Rate");
...and my view is (currently):
<div class="editor-field">
#Html.DropDownList("StandID", "--Select--")
</div>
...but the "Description" + "-- £" + "Rate"); won't run:
DataBinding:
'System.Data.Entity.DynamicProxies.Stand_63F8C9F623B3C0E57D3008A57081AFCD9C39E1A6B79B0380B60840F1EFAE9DB4'
does not contain a property with the name 'Description--£Rate'.
Thanks for any help,
Mark
You could create a new anonymous class using a simple LINQ projection, and then use the SelectList(IEnumerable, string, string) constructor overload to specify the value and text fields to be used for the <option> elements i.e.:
var stands =
db.Stands
.Where(s => s.ExhibitorID == null)
.Select(s => new
{
StandID = s.StandID,
Description = string.Format("{0}-- £{1}", s.Description, s.Rate)
})
.ToList();
ViewBag.StandID = new SelectList(stands, "StandID", "Description")
Edit
In C#6 and later, string interpolation makes for better reading than string.Format
...
Description = $"{s.Description}-- £{s.Rate}"
If you project to a strong ViewModel class name (instead of to an anonymous class), you will undoubtedly want to replace the magic strings with the safety of the nameof operator:
ViewBag.StandID = new SelectList(stands, nameof(Stand.StandID), nameof(Stand.Description));
var stands = db.Stands.Where(s => s.ExhibitorID == null).ToList();
IEnumerable<SelectListItem> selectList = from s in stands
select new SelectListItem
{
Value = s.StandID,
Text = s.Description + "-- £" + s.Rate.ToString()
};
ViewBag.StandID = new SelectList(selectList, "Value", "Text");
You can create a partial Model class
public partial class Stand
{
public string DisplayName
{
get
{
return this.Description + "-- £" + this.Rate.ToString();
}
}
}
Then in your View
var stands = db.Stands.Where(s => s.ExhibitorID == null).ToList();
ViewBag.StandID = new SelectList(stands,"StandID", "DisplayName");
The Format of the constructor that you are using is
SelectList(IEnumerable items, string dataValueField, string dataTextField).
So when you use it the way you have you are actually telling it to bind to the TextField called "Description-- £Rate" and if this is not what the field is called coming in the from the DB it won't know what you are indicating.
Either of the two methods described above will work as long as the value you have in your dataValueField matches the name of the property you put the Value in and the dataTextField matches the property name of where you put the Text, perhaps a mix of the two solutions above. (Only because I prefer lambda expressions over linq.) and using a selectlist item prevents it from have to do a ToList on the collection after the transform. you are actually creating the objects that naturally bind to a select list.
You also may want to put in checks on the description or rate to make sure they aren't empty before putting them into the list
var stands = db.Stands.Where(s => s.ExhibitorID == null)
.Select(s => new SelectListItem
{
Value = s.StandID.ToString(),
Text = s.Description + "-- £" + s.Rate.ToString()
});
ViewBag.StandID = new SelectList(stands, "Value", "Text");
I did this by modifying my View Model, here are my code:
The View Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcEsosNew.Models;
using System.Web.Mvc;
namespace MvcEsosNew.ViewModels
{
public class EntitlementViewModel
{
public int EntitlementCount { get; set; }
public Entitlement Entitlement { get; set; }
public SelectList Member { get; set; }
public SelectList Job_Grade { get; set; }
public SelectList Department { get; set; }
public SelectList Esos_Batch { get; set; }
}
public class department_FullName
{
public int deptID { get; set; }
public string deptCode { get; set; }
public string deptName { get; set; }
public string fullName { get { return deptCode + " - " + deptName; } }
}
}
The Controller
public void getAllDepartment(EntitlementViewModel entitlementVM)
{
var department = from Department in db.Departments.Where(D => D.Status == "ACTIVE").ToList()
select new department_FullName
{
deptID = Department.id,
deptCode = Department.department_code,
deptName = Department.department_name
};
entitlementVM.Department = new SelectList(department, "deptID", "fullName");
}
The View
<div class="form-group row">
<div class="col-sm-2">
#Html.LabelFor(model => model.Entitlement.department_id)
</div>
<div class="col-sm-10">
#Html.DropDownListFor(model => model.Entitlement.department_id, Model.Department, new { #class="form-control" })
#Html.ValidationMessageFor(model => model.Entitlement.department_id)
</div>
</div>
The result:
In one of my first MVC projects I encountered the following part of code in my "Create" view.
<div class="form-group">
#Html.LabelFor(model => model.Attend, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
#Html.EditorFor(model => model.Attend)
#Html.ValidationMessageFor(model => model.Attend, "", new { #class = "text-danger" })
</div>
</div>
</div>
This was scaffolded based on the following property in my model:
public virtual bool? Attend { get; set; }
What this will show for me on the "Create" view is a dropdown with the following values: Not Set; True; False;
Is there a way I can change these values in the dropdownlist for my bool so that it says something more readable/understandable for a user like: Yes; No;?
I tried to search for a solution and I think it has something to do with the following line:
#Html.EditorFor(model => model.Attend)
As #Stephen Muecke said you can create your own selectlist. You can do it with your own editor template .
Here is how i have done it:
Create EditorTeplates folder in Views/Shared
Create YesNoNotSelected.cshtml in this folder with this code:
#model bool?
#Html.DropDownList("", new SelectListItem[]
{
new SelectListItem()
{
Text = "Not set",
Value = String.Empty,
Selected = !Model.HasValue
},
new SelectListItem()
{
Text = "True",
Value = "true",
Selected = Model.HasValue && Model.Value
},
new SelectListItem()
{
Text = "False",
Value = "false",
Selected = Model.HasValue && !Model.Value
}
})
In your model add UIHintAttribute above Attend property like this:
[UIHint("YesNoNotSelected")]
public virtual bool? Attend { get; set; }
Now EditorFor helper should render your property according to this template.
I like the best of all worlds scenario:
I don't want to always require a UIHint
I don't want to use UIHint at all, it's not descriptive
I want bools to be extensible; yes/no, true/false, etc
Here is how I implemented it:
/Models/BooleanType.cs
public enum BooleanDisplay
{
undefined = 0,
YesNo = 1,
TrueFalse = 2,
GoodEvil = 3
}
/MVC/UIBooleanType.cs
[AttributeUsage(AttributeTargets.Property)]
public class UIBooleanAttribute : UIHintAttribute
:base("boolean", "MVC")
{
public UIBoolean(BooleanType displayAs)
{
DisplayAs = displayAs;
}
public BooleanDisplay DisplayAs { get; set; }
}
/Views/Shared/EditorTemplates/boolean.cshtml
#model boolean?
#{
// default
var displayAs = BooleanDisplay.TrueFalse;
var uiBoolean = ViewData
.ModelMetadata
.ContainerType
.GetProperty(ViewData.ModelMetadata.PropertyName)
.GetCustomAttributes(typeof(UIBooleanAttribute))
.Select(ca => ca as UIBooleanAttribute)
.FirstOrDefault(ca => ca != null);
if (uiBoolean != null)
{
displayAs = uiBoolean.DisplayAs;
}
var values = new List<SelectListItem>()
{
new SelectListItem()
{
Text = "Not set",
Value = String.Empty,
Selected = !Model.HasValue
}
};
switch(displayAs)
{
default:
throw new NotImplementedException(displayAs.ToString()
+ " is not implemented in boolean.cshtml";
YesNo:
values.Add(new SelectListItem()
{
Text = "Yes",
Value = "true",
Selected = Model.HasValue && Model.Value
});
values.Add(new SelectListItem()
{
Text = "No",
Value = "false",
Selected = Model.HasValue && !Model.Value
});
TrueFalse:
// etc
}
}
#Html.DropDownList("", values)
usage:
ViewModel:
public class Person
{
// defaults to True/False
public bool AreYouHappy { get; set; }
[UIBoolean(BooleanDisplay.GoodEvil)]
public bool IsInherently { get; set; }
}
View:
#model Person
#Html.EditorFor(p => p.AreYouHappy)
#Html.EditorFor(p => p.IsInherently)
I previously had the following line of code from within my AdminController that was successfully returning a list of relevant subsections from within a course:
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetCourseSections(int courseID)
{
var Sections = dbcontext.CourseSection.Where(cs => cs.CourseID.Equals(courseID)).Select(x => new
{
sectionID = x.CourseSectionID,
sectionTitle = x.Title
);
return Json(Sections, JsonRequestBehavior.AllowGet);
}
I was informed to take this out of the controller as it was bad practice to call dbcontext and so i moved this to the AdminViewModel. Within my AdminViewModel I have a variable public List CourseSectionList { get; set; } and I am trying to populate this variable with the JSON request details. My code is as follows:
AdminViewModel
public void GetCourseSectionDetails(int courseID)
{
var Sections = dbcontext.CourseSection.Where(cs => cs.CourseID.Equals(courseID)).Select(x => new CourseSection
{
CourseSectionID = x.CourseSectionID,
Title = x.Title
});
this.CourseSectionList = Sections.ToList();
}
AdminController
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetCourseSections(int courseID)
{
avm.GetCourseSectionDetails(courseID);
var Sections = avm.CourseSectionList.Where(cs => cs.CourseID.Equals(courseID)).Select(x => new
{
sectionID = x.CourseSectionID,
sectionTitle = x.Title
});
System.Diagnostics.EventLog.WriteEntry("Application", "JSON=" + Sections.ToList(), System.Diagnostics.EventLogEntryType.Error);
return Json(Sections, JsonRequestBehavior.AllowGet);
}
I am getting the error The entity or complex type 'MetaLearning.Data.CourseSection' cannot be constructed in a LINQ to Entities query. How can I populate this.CourseSectionList variable using the Sections?
As pointed by your error message, you can't, in linq to entities, use a
.Select(m => new <Entity>{bla bla})
where <Entity>... is one of your model's entity.
So either you use a "non model" class (DTO), which has the properties you need, or you have to enumerate before selecting (because linq to objects has not that limitation)
.ToList()
.Select(m => new <Entity>{bla bla});
You can find some nice explanations of why it's not possible here
EDIT :
you may also do something like that, if you wanna retrive only some properties of your entity, and don't wanna use a DTO :
return ctx
.CourseSection
.Where(cs => cs.CourseID.Equals(courseID))
//use an anonymous object to retrieve only the wanted properties
.Select(x => new
{
c= x.CourseSectionID,
t= x.Title,
})
//enumerate, good bye linq2entities
.ToList()
//welcome to linq2objects
.Select(m => new CourseSection {
CourseSectionID = m.c,
Title = m.t,
})
.ToList();
You don't need to repeat the same code in the controller, but directly pass the list to the view.
This being said I am informing you that placing data access code in your view model is even worse practice than keeping it in the controller. I would recommend you having a specific DAL layer:
public interface IRepository
{
public IList<CourseSection> GetSections(int courseID);
}
which would be implemented:
public class RepositoryEF : IRepository
{
public IList<CourseSection> GetSections(int courseID)
{
using (ctx = new YourDbContextHere())
{
return ctx
.CourseSection
.Where(cs => cs.CourseID.Equals(courseID))
.Select(x => new CourseSection
{
CourseSectionID = x.CourseSectionID,
Title = x.Title,
})
.ToList();
}
}
}
and finally have your controller take the repository as dependency:
public class SomeController : Controller
{
private readonly IRepository repo;
public SomeController(IRepository repo)
{
this.repo = repo;
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetCourseSections(int courseID)
{
var sections = this.repo.GetSections(courseID);
return Json(sections, JsonRequestBehavior.AllowGet);
}
}
I did this as follows using Darin's answer as a guide:
ViewModel
public void GetCourseSectionDetails(int courseID)
{
this.CourseSectionList = dbcontext.CourseSection.AsEnumerable().Where(cs => cs.CourseID.Equals(courseID)).Select(x => new CourseSection
{
CourseSectionID = x.CourseSectionID,
Title = x.Title
}).ToList();
}
Controller
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult GetCourseSections(int courseID)
{
var sections = avm.CourseSectionList;
return Json(sections, JsonRequestBehavior.AllowGet);
}
I'm trying to group together radiobuttons that are creating using a for loop and Razor syntax. Here is the code:
#for (var i = 0; i < Model.Sessions.Count(); i++)
{
#Html.HiddenFor(it => it.Sessions[i].Id)
#Html.RadioButtonFor(it => it.Sessions[i].Checkbox, "0", new {#class = "Sessions", #id = id, #name="Sessions"})
#Html.LabelFor(it => it.Sessions[i].Name, Model.Sessions[i].Name)
<span class="time-span"><em>#Model.Sessions[i].StartTime</em><em>#Model.Sessions[i].EndTime</em></span>
<br />
}
The third line inside the for loop is where the problem is. Basically the name doesn't change and it's always "Sessions[x].Checkbox". The checkbox is a property (bool) of a custom class. I can't seem to get the hang of debugging Razor stuff, so any help would be greatly appreciated, I'm guessing this will be extremely obvious to someone here.
EDIT
Dimitrov's post helped a lot. Below is the final code I used. I use the #class and #id attributes to be able to use Javascript to select the session originally picked (since this is an edit, not create form).
#for (var i = 0; i < Model.Sessions.Count(); i++)
{
#Html.HiddenFor(it => it.Sessions[i].Id)
var SId = #Model.Sessions[i].Id;
#Html.RadioButtonFor(it => it.selectedSession, Model.Sessions[i].Id, new { id = SId, #class = "Sessions" })
#Html.LabelFor(it => it.Sessions[i].Name, Model.Sessions[i].Name)
<span class="time-span"><em>#Model.Sessions[i].StartTime</em><em>#Model.Sessions[i].EndTime</em></span>
<br />
}
If you want to be able to select only a single radio button you need to have a single property on your view model to hold the selected session id, like this:
public class SessionViewModel
{
public int SelectedSessionId { get; set; }
public IList<Session> Sessions { get; set; }
}
public class Session
{
public int Id { get; set; }
public string Name { get; set; }
}
and then have a controller:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new SessionViewModel
{
SelectedSessionId = 2,
Sessions = Enumerable.Range(1, 5).Select(x => new Session
{
Id = x,
Name = "session" + x,
}).ToList()
};
return View(model);
}
[HttpPost]
public ActionResult Index(SessionViewModel model)
{
return Content("Thank you for selecting session id: " + model.SelectedSessionId);
}
}
and finally a view:
#model SessionViewModel
#using (Html.BeginForm())
{
for (var i = 0; i < Model.Sessions.Count(); i++)
{
#Html.HiddenFor(x => x.Sessions[i].Id)
#Html.RadioButtonFor(x => x.SelectedSessionId, Model.Sessions[i].Id, new { id = "session_" + i })
#Html.Label("session_" + i, Model.Sessions[i].Name)
<br/>
}
<button type="submit">OK</button>
}