Menu
  • HOME
  • TAGS

N tier architecture c# mvc ViewModels

c#,asp.net-mvc,n-tier,n-tier-architecture,asp.net-mvc-viewmodel

[...] and the query should not return any extra field. Only the fields i specified in my viewmodel. Actually returning an instance of some model, domain object or whatever in your domain layer which has 10, 20 or 100 properties shouldn't mean that all properties must be set. In...

ViewModels in ASP.NET MVC - best practices?

c#,asp.net-mvc,asp.net-mvc-viewmodel

is it acceptable for a ViewModel to contain instances of domain models basically not, although I see it happen a lot. It depends a bit on the quick-win level of your project. Let me give an example, imagine the following view model: public class FooViewModel { public string Name {get;...

MVC - Populate Two Dropdowns From Database in Partial View with ViewModel

c#,asp.net-mvc,linq,asp.net-mvc-partialview,asp.net-mvc-viewmodel

You need to create an instance of a viewmodel, populate it and pass it to your PartialView: [ChildActionOnly] public ActionResult TeamAssignment() { // Instantiate A VIEW MODEL HERE TO PASS INTO THE PARTIALVIEW MyCustomModel model = new MyCustomModel(); model.nameList = (from p in db.Participant where p.ParticipantType == "I" select p).ToList();...

Calculated Property in MVC 5 ViewModel instead of Model Class

c#,asp.net-mvc,razor,model,asp.net-mvc-viewmodel

A view model should not contain any database logic. It should have simple properties only. Also there is no way that you could use this in any edit scenario where you need to post back because a) you do not have a parameterless constructor (will throw an exception) and you...

Data validation for every item in a list of my ViewModel

c#,validation,custom-attributes,asp.net-mvc-viewmodel

There isn't really a way for Data Annotations to apply to the elements of a list. What you would have to do is create a wrapper class and apply the Data Annotation to the elements in the wrapper class, like so: public IList<MyField> MyFields {get;set;} public class MyField { [RegularExpression("MyRegex",...

Passing a dictionary to a controller

c#,asp.net-mvc-4,razor,asp.net-mvc-viewmodel

MVC already puts everything that was submitted into a dictionary for you. public ActionResult BulkEditStudentRecordsAdultEd(FormCollection vm) { // to access an item, do this: vm["inputName"] } If you want the default modelbinder to bind for you into a different dictionary, you can do so by having inputs with correct names:...

MVC 5 routing issue with new url for subcategories

asp.net-mvc-5,asp.net-mvc-routing,razor-2,asp.net-mvc-viewmodel

In your BeginForm(...) you end up having to pass a route values dictionary with Subcategory = "runningshoes" This does mix values being passed by GET, aka in the querystring through the route values dictionary, and POST, which will be the values from the form, but will accomplish what you are...

Pass List data along with ViewModel to view and populate list data in drop down

c#,asp.net-mvc,entity-framework,asp.net-mvc-viewmodel

Since DropDownList uses IEnumerable<SelectListItem>, change the view model to public class CompanyProfileModelView { public Company Company { get; set; } public SelectList CategoryList { get; set; } } and assuming Company model contains [Display(Name="Category")] public int? CategoryType { get; set; } Controller [HttpGet] public ActionResult CreateCompany() { var listData =...

how to show already existed permission using checked checkbox in Edit page

asp.net-mvc-4,checkbox,asp.net-mvc-viewmodel

try this for Edit Controller , and rests are Fine [HttpGet] public ActionResult RoleEdit(int id, ViewModelRole viewModelRole) { viewModelRole.Role = roleService.GetRole(id); var allPermission = tDbContext.Permissions; var rolePermissions = (from p in preFlightDbContext.Permissions join rd in tDbContext.RoleDetails on p.PermissionId equals rd.PermissionId where rd.RoleId == id select p.PermissionId).Distinct(); var viewModel = new...

ASP.NET MVC Register custom Model Binder for all ViewModels

asp.net,asp.net-mvc,asp.net-mvc-viewmodel

As it was commented in my original post you can register all the model blinders by replacing the default binder ModelBinders.Binders.DefaultBinder = new ContextDataBinder(); However that didn't worked for my specific case because the ViewModels that the framework creates automatically always return the previous ViewModel (in searches for example it...

ASP.NET MVC Details view with many display fields but a single editable field

asp.net-mvc,entity-framework-6,repository-pattern,asp.net-mvc-viewmodel

So I solved this by ensuring that the primary key field PersonID is included in the View. I didn't think I needed it originally because it started off as a read-only Details view, and PersonID wasn't needed to be displayed. But when posting data back, I needed to add it...

MVC Mapping between View Model and Request-Response messaging pattern

asp.net-mvc,design-patterns,automapper,asp.net-mvc-viewmodel

Request/Response is a messaging pattern, but it appears you are not using messages.. instead you are using objects. This is the real crux of your problem. You're using the pattern incorrectly, and more importantly, it seems like you're using the wrong pattern for the job. Why do you need messaging...

MVC 5 - Pass object to a shared view

redirect,asp.net-mvc-5,asp.net-mvc-viewmodel,asp.net-mvc-views,onexception

I don't think you can return view as you want, so I usually put values into TempData and make a redirection to homepage or whatever landing page. Homepage check is there is value into this Viewbag and show error if there is error. Controller: public class BaseController : Controller {...

Show Files trough viewmodel isn't possible because of static types cannot be used as type arguments

c#,asp.net-mvc,mvvm,viewmodel,asp.net-mvc-viewmodel

Your problem is exactly what error say. You can't use static class as property type. If you really want to set file to ViewModel you should use FileResult or FileContentResult or just byte[].

Doubles from ViewModel get rounded to ints in javascript array

javascript,arrays,razor,rounding,asp.net-mvc-viewmodel

Float values passed to JavaScript as JSON or direct values must have "." as decimal separator. Some cultures use "," and thus parsing of such numbers as JSON or with parseFloat will fail to recognize decimal part. Fix: use invariant culture to format numbers or use existing libraries (like Json.Net)...

MVC 5 ViewBag security

security,asp.net-mvc-5,viewbag,asp.net-mvc-viewmodel,asp.net-mvc-views

Use your view model. When the ViewBag was implemented (MVC 3) dynamic typing was new (.NET 4.0) and it was put in just as a side-option to ViewData or to quickly generate a view without the need for additional classes. Any serious MVC project will take advantage of a model/viewmodel...

Do I have to implement a ViewModel when using multiple Models in one View?

c#,asp.net-mvc-4,asp.net-mvc-viewmodel

100% you should create a viewmodel that comprises of the two models you need. public class MainViewModel { public ModelA ModelA { get; set; } public ModelB ModelB { get; set; } } You should keep the association of one view -> one viewmodel class. I keep to this in...

How use viewModels + Entity Framework? Is necessary add a migration?

c#,asp.net-mvc,entity-framework,viewmodel,asp.net-mvc-viewmodel

Migrations are used when you change database schema (= you change the classes that are used as models/entitites for your database). When you got empty up() and down() it means there were not any changes related to you entities classes. So if you create database, then add some property for...

Pass list value to view model class variable in ASP.NET MVC

c#,asp.net-mvc,repository-pattern,asp.net-mvc-viewmodel

I would have done it a little different. You want to return a single view model to the view consisting of a list of users and a list of groups (please correct me if I am wrong). I would have initialised the lists to empty (containing no items) in the...

Access model variables in entire project MVC5 Razor - use Basecontroller to set BaseViewModel and use that in Layout

c#,asp.net-mvc,razor,asp.net-mvc-5,asp.net-mvc-viewmodel

I use a similar approach than you in CRUD views to pass to the view flags with user permision (CanCreate, CanUpdate, etc). But in this case I know when to do it. In your case is application wide. You can try this: public override void OnActionExecuting(ActionExecutingContext filterContext) { var controller...

.NET MVC — Use a class as the model?

c#,asp.net-mvc,class,asp.net-mvc-viewmodel

Create a Base class and then using inheritance, you have access to those common properties. public class AccountProfile { public string FirstName { get; set; } public string Lastname { get; set; } } public class OtherClass : AccountProfile { //here you have access to FirstName and Lastname by inheritance...

How do I get this ViewModel correct?

asp.net-mvc,entity-framework,linq-to-entities,asp.net-mvc-viewmodel

You should use SelectMany: viewModel.Goals = viewModel.EmployeeMaps .Where(e => e.EmployeeID == ViewBag.EmployeeID) .SelectMany(e => e.Goals); because EmployeeMaps.Where() is an IEnumerable, so a Select produces IEnumerable<IEnumerable<Goal>>. SelectMany flattens this into IEnumerable<Goal>....

How to store values from ViewModel for use in POST

asp.net-mvc-viewmodel

HTML inputs, in general, are your only option, hidden or otherwise. As far a security risks go, the only possible problem is that the values of these inputs can be changed; even if you use hidden inputs, an enterprising user can use the developer tools of their browser to change...

Can I determine the model of a strongly typed ASP.NET MVC view before it is executed?

asp.net-mvc,asp.net-mvc-viewmodel

This will do the trick (I verified this in a controller, but it can run in a filter as well). ** Note this works for the default view engine/default view page and might need tweaking otherwise, it's not hardened by any means, it's just to show the pattern Type modelType...

MVC View ViewModel HttpPost return value is always NULL

asp.net-mvc,asp.net-mvc-4,razor,asp.net-mvc-viewmodel

I was able to reproduce your issue and was confused because I know that the default MVC Model Binder understands complex types. I stripped away most of the code and just tried to do it with the Company object, which still failed. I then noticed that in vmCompanyAddress that the...

Save List View Models to Database

c#,asp.net-mvc,asp.net-mvc-viewmodel

In the foreach loop you need to initialize a new Company foreach (var item in model.Companies) { Company company = new Company() { CompanyName = item.CompanyName, JobTitle = item.JobTitle, // etc for other properties }; cv.Companies.Add(company); } ...

Null Reference Error when directly assigning a List object to an PagedList

asp.net-mvc,linq,nullreferenceexception,asp.net-mvc-viewmodel,pagedlist

Your code as I understand it: KeyMasterViewModel keyMasterViewModel = new KeyMasterViewModel(); [...] keyMasterViewModel.pagedkeymodel.pagedkeymaster = KeyMasterModelObject; So here you are using keyMasterViewModel.pagedkeymodel and assuming it is not null (because you are trying to assign to a property on it). However KeyMasterViewModel doesn't have a constructor and doesn't set pagedkeymodel to anything...

How do I pass multiple objects from a ViewModel to a jQuery function?

c#,jquery,asp.net-mvc-5,asp.net-mvc-viewmodel,asp.net-mvc-views

You can serialize your list and storage it in a hidden field. Then call LoadMapMarker by means of Javascript on client side. Server: using System.Web.Script.Serialization; var MapMarkers = new List<MapMarker>(); var jsonSerialiser = new JavaScriptSerializer(); var json = jsonSerialiser.Serialize(MapMarkers); return View(new MyViewModel({JsonList = json })); View: <input type="hidden" id= "MyHiddenFieldForMapMarker"...