viewmodel

Why do I get null instead of empty string when receiving POST request in from Razor View?

大憨熊 提交于 2019-11-27 17:56:26
I used to receive empty string when there was no value: [HttpPost] public ActionResult Add(string text) { // text is "" when there's no value provided by user } But now I'm passing a model [HttpPost] public ActionResult Add(SomeModel Model) { // model.Text is null when there's no value provided by user } So I have to use the ?? "" operator. Why is this happening? Michael Jubb You can use the DisplayFormat attribute on the property of your model class: [DisplayFormat(ConvertEmptyStringToNull = false)] The default model binding will create a new SomeModel for you. The default value for the

ASP.Net MVC and state - how to keep state between requests

陌路散爱 提交于 2019-11-27 17:26:32
As a fairly experienced ASP.Net developer just recently starting using MVC, I find myself struggling a bit to change my mindset from a traditional "server control and event handler" way of doing things, into the more dynamic MVC way of things. I think I am slowly getting there, but sometimes the MVC "magic" throws me off. My current scenario is to create a web page that allows the user to browse a local file, upload it to the server and repeat this until he has a list of files to work with. When he is happy with the file list (which will be displayed in a grid on the page), he will click a

DTO = ViewModel?

非 Y 不嫁゛ 提交于 2019-11-27 17:04:23
I'm using NHibernate to persist my domain objects. To keep things simple I'm using an ASP.NET MVC project as both my presentation layer, and my service layer. I want to return my domain objects in XML from my controller classes. After reading some posts here on Stack Overflow I gather DTOs are the way to go. However, I've also come across posts talking about the ViewModel. My question: Are Data Transfer Objects and ViewModels the same thing? Or is a ViewModel a kind of sub pattern of a DTO? The canonical definition of a DTO is the data shape of an object without any behavior. ViewModels are

MVC - Create object and related objects in one go

浪尽此生 提交于 2019-11-27 16:50:27
问题 I want to create a parent object with child/related objects in the same view. An example would be: create one Father (with some name) along with all his sons (with their names). I have created a view model: public class FatherViewModel { public Father father {get; set;} // has 1 property Name public List<Son> {get; set;} // has 1 property Name } My question is, how do I get the list of Sons back from the view when the post is performed? I have tried using HiddenFor for each Son id, but no

How to map View Model back to Domain Model in a POST action?

爷,独闯天下 提交于 2019-11-27 16:38:54
Every article found in the Internet on using ViewModels and utilizing Automapper gives the guidelines of the "Controller -> View" direction mapping. You take a domain model along with all Select Lists into one specialized ViewModel and pass it to the view. That's clear and fine. The view has a form, and eventually we are in the POST action. Here all the Model Binders come to the scene along with [obviously] another View Model which is [obviously] related to the original ViewModel at least in the part of naming conventions for the sake of binding and validation. How do you map it to your Domain

Using multiple models in a single controller [closed]

有些话、适合烂在心里 提交于 2019-11-27 16:29:21
I have used EF Designer from database to build my models from a database created for my project. For a single controller, I need to reference multiple models. An example of this is that there is a Users table which contains the user department and office. I need to reference two separate tables DepartmentPermissions and OfficePermissions to determine what data the user is able to see. Here are examples of the auto generated models: //------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual

How does DataAnnotationsModelBinder work with custom ViewModels?

瘦欲@ 提交于 2019-11-27 15:20:20
问题 I'm trying to use the DataAnnotationsModelBinder in order to use data annotations for server-side validation in ASP.NET MVC. Everything works fine as long as my ViewModel is just a simple class with immediate properties such as public class Foo { public int Bar {get;set;} } However, the DataAnnotationsModelBinder causes a NullReferenceException when trying to use a complex ViewModel , such as public class Foo { public class Baz { public int Bar {get;set;} } public Baz MyBazProperty {get;set;}

MVC3 multi step form - How to persist model object

时间秒杀一切 提交于 2019-11-27 15:16:37
问题 I have a multi step form which uses one model object and I need to persist it between the steps. The object gets saved to the database only after the final step. I have seen people suggest using HTML.Serialize but how secure is this option? Also my model object will grow as the user fills up the form which means the hidden form field with serialized data will add up size to my HTML output. Whats the best practice for this kind of situation? 回答1: I've use the TempData for this purpose. You can

Room - LiveData observer does not trigger when database is updated

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 14:41:48
I am trying to find out in the code below, why is it that Room's LiveData observable does not give me new shifts once I populate the database with new data. This is put on my activity's onCreate method: shiftsViewModel = ViewModelProviders.of(this).get(ShiftsViewModel.class); shiftsViewModel .getShifts() .observe(this, this::populateAdapter); This is the populateAdapter method: private void populateAdapter(@NonNull final List<Shift> shifts){ recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(shifts)); } I also have the following code that populates the database (I use RxJava to do the

Maintain state of a dynamic list of checkboxes in ASP.NET MVC

橙三吉。 提交于 2019-11-27 14:08:36
I have a class called "PropertyFeature" which simply contains PropertyFeatureID and Description. It's a proper model created through LINQ to SQL mapped to an SQL Server database table. An example instance/row would be: PropertyFeatureID: 2 Description: "Swimming Pool" The number of rows (PropertyFeatures) can of course grow and shrink, and so I want to dynamically render a list of checkboxes so that the user can select any of them. I can dynamically render the Checkboxes easily enough, with something like: <%foreach (var Feature in (ViewData["Features"] as IEnumerable<MySolution.Models