asp.net-mvc-viewmodel

Is it possible to reuse the DataAnnotations in ViewModel?

社会主义新天地 提交于 2019-12-05 04:41:32
问题 In my MVC application, I defined the DataAnnotations in the domain models. Although the DataAnnotations properties as Display, etc. can be retrieved when using Domain model, they cannot be retrieved when using the same properties on ViewModel and using this ViewModel. I think it is not seem to good to define the DataAnnotations in ViewModel again. So, is it possible or which way should I follow? Domain Model: public class Issue { [Key] public int ID { get; set; } [Required(ErrorMessage =

MVC validation lost in Knockoutjs post

有些话、适合烂在心里 提交于 2019-12-04 11:43:27
I'm using MVC4 and knockout. I have a form on a page that is strongly typed to a viewmodel. In that viewmodel I have some validation defined, for instance: [Required(ErrorMessage = "Title is required")] public string Title { get; set; } If I do a standard post to the form without the required field filled, my action sees that the model is not valid and returns to the view, and a main validation message is displayed because I have @Html.ValidationSummary in my form. The individual field is also marked as invalid (with a message) because I have @Html.ValidationMessageFor associated with the

Is it possible to reuse the DataAnnotations in ViewModel?

懵懂的女人 提交于 2019-12-03 20:35:14
In my MVC application, I defined the DataAnnotations in the domain models. Although the DataAnnotations properties as Display, etc. can be retrieved when using Domain model, they cannot be retrieved when using the same properties on ViewModel and using this ViewModel. I think it is not seem to good to define the DataAnnotations in ViewModel again. So, is it possible or which way should I follow? Domain Model: public class Issue { [Key] public int ID { get; set; } [Required(ErrorMessage = "Required")] [Display(Name = "Project Number")] public int ProjectID { get; set; } [Required(ErrorMessage =

Show multiple models in a single view using ViewModel in Razor MVC3 (with only Details in view)

给你一囗甜甜゛ 提交于 2019-12-01 18:35:24
My task is to show multiple models into a single view.I've created a ViewModel for my requirement but I'm not meeting my requirement. please have a look into the below code and rectify me where m i going wrong ??? public partial class StudentsDetail { public int StudentID { get; set; } public int ParentID { get; set; } public string StudentName { get; set; } public string Gender { get; set; } public string FatherName { get; set; } public string MotherName { get; set; } public Nullable<System.DateTime> DateOfBirth { get; set; } public virtual ParentsDetail ParentsDetail { get; set; } public

MVC ViewModel Binding Construction vs. Flattening

拜拜、爱过 提交于 2019-12-01 09:06:49
问题 In my ViewModel (also in my Domain model), I have kinda dynamic Property Structure where the Profile Elements are a List of the base class ProfileVM and refer to a ProfileDefinitionElement (just to explain the ViewModel without pasting the full thing). public class OwnProfileVM { public OwnProfileVM() {} public ProfileDefinitionVM ProfileDefinitionVM { get; set; } public ProfileVM ProfileVM { get; set; } } So I bind my Properties using a Linq Single statement: @Model.ProfileDefinitionVM

Inserting multiple entities into many-to-many linking table

梦想的初衷 提交于 2019-11-29 18:15:33
My web application has a many-to-many link between the Station and Timetable entities, and the entity that links them is called StationTimetable , which also holds data about the linkage between the two entities. When I create a new Timetable , I want to create new (and multiple) StationTimetable entries in the database to correspond to this new Timetable . So far I've managed to get the application to save just one StationTimetable entry when the user creates a new timetable. I sort of understand why this is, and I've attempted to modify it to save multiple entries however I'm now hitting a

ViewModels with asp.net mvc 4 and EntityFramework whats the Point

Deadly 提交于 2019-11-29 10:51:19
I'm debating with myself whats the point in creating ViewModel classes in a project which uses Entity Framework? I currently have a project which uses EntityFramework. My solution is structured basically like this: UI project (contains controllers and Views) Model project (contains EntityFramework model) Services project (contains service classes which talk to Model Project to serve up the entities out of the model project to the UI project) My controllers pass the entities that Entity Framework creates straight to the view. It's nice and simple. In the past I would of created separate view

How to pass model to partial view

孤街醉人 提交于 2019-11-29 02:51:04
I have two view models: public class ParentViewModel { public Id { get; set; } ..... public ChildViewModel Child{ get; set; } } public class ChildViewModel { public ChildId { get; set; } ..... } Controllers: public ActionResult Index() { .... <some code> return View("NewIndex", ParentViewModel); } [HttpPost] public ActionResult PartialAction(ChildViewModel childView) { return RedirectToAction("Index"); } And views: Index @model ParentViewModel .... @Html.Partial("_Partial", Model.Child) and _Partial @model ChildViewModel ... do some stuff with child model When I'm trying to open Index page I

ViewBag vs Model, in MVC.NET [closed]

牧云@^-^@ 提交于 2019-11-28 10:56:56
This is more of a generic architectural question: I'm trying to decide if it's ok for my programmers to use "ViewBags" to pass data to views that already accept Models. My personal preference is to avoid ViewBags and build Robust Models containing all the data the view requires: Approach 1: MODEL A: - List of Employees - Nullable integer, indicating which item from the list is currently selected - string firstName (empty if index is null) - string lastname (empty if index is null) Approach 2: MODEL A: - List of Employees ViewBag: - ViewBag.Index (indicating which item from the list is

MVC - Passing multiple data tables to a view

大憨熊 提交于 2019-11-28 09:23:29
I currently have the following code in the HomeController of my MVC project: public class HomeController : Controller { public ActionResult Index() { MyDataContext dc = new MyDataContext(); IQueryable<Table1Data> j = from n in dc.Table1 select n; return View(j); } So that works okay, but now I want to pass a second table through to the same view. So I was thinking I should be able to do something like this: public class HomeController : Controller { public ActionResult Index() { MyDataContext dc = new MyDataContext(); IQueryable<Table1Data> j = from n in dc.Table1 select n; IQueryable