All Questions
Tagged with model asp.net-mvc
1,092
questions
195
votes
11
answers
435k
views
Accessing MVC's model property from Javascript
I have the following model which is wrapped in my view model
public class FloorPlanSettingsModel
{
public int Id { get; set; }
public int? MainFloorPlanId { get; set; }
public string ...
141
votes
8
answers
153k
views
ASP.NET MVC Yes/No Radio Buttons with Strongly Bound Model MVC
Does anyone know how to bind a Yes/No radio button to a boolean property of a Strongly Typed Model in ASP.NET MVC.
Model
public class MyClass
{
public bool Blah { get; set; }
}
View
<%@ ...
96
votes
5
answers
88k
views
ASP.NET MVC Model vs ViewModel
OK, I have been hearing discussion about "ViewModels" in regards to MS's ASP.NET MVC.
Now, that is intended to be a specific kind of Model, correct? Not a specific kind of View.
To my understanding,...
94
votes
5
answers
39k
views
DisplayNameFor() From List<Object> in Model
I believe this is pretty simple, I just can't seem to find the right way to show the display name for an item within a list within my model.
My simplified model:
public class PersonViewModel
{
...
65
votes
4
answers
122k
views
Email model validation with DataAnnotations and DataType
I have following model:
public class FormularModel
{
[Required]
public string Position { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
...
51
votes
5
answers
42k
views
How to add boolean required attribute in mvc?
I have one model class like:
public class Student
{
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "...
44
votes
4
answers
138k
views
MVC 4 - how do I pass model data to a partial view?
I'm building a profile page that will have a number of sections that relate to a particular model (Tenant) - AboutMe, MyPreferences - those kind of things. Each one of those sections is going to be a ...
36
votes
4
answers
20k
views
Required Attribute on Generic List Property
Is it possible to put a [Required] attribute onto a List<> property?
I bind to a generic list on POST and was wondering if I could make ModelState.IsValid() fail if the property has 0 items in it?
31
votes
2
answers
42k
views
Real example of TryUpdateModel, ASP .NET MVC 3
I can't understand, how to use TryUpdateModel and save the MVC architecture at the same time.
If I am not mistaken, work with datacontexts must be in the Model. So, such code
var db=new ...
30
votes
4
answers
25k
views
How to set default value of TextBox empty string instead of null
I may be out of date, but one principle I adhere to is avoid nulls as much as possible.
However what I have found is that for a strongly typed view in which the user inputs the properties of an ...
30
votes
5
answers
49k
views
MVC Validation Lower/Higher than other value
How is the best way to validate a model in MVC.Net where I want to accept a minimum/maximum.
Not individual min/max values for a field. But separate fields for a user to specify a minimum/maximum.
...
29
votes
4
answers
89k
views
MVC 4 - Use a different model in partial view
Please bear with my noobness, I'm super new to the MVC pattern.
What I'm trying to do
I am building a profile information page for registered users on my site. This page would list data about the ...
28
votes
5
answers
11k
views
getting the values from a nested complex object that is passed to a partial view
I have a ViewModel that has a complex object as one of its members. The complex object has 4 properties (all strings). I'm trying to create a re-usable partial view where I can pass in the complex ...
27
votes
5
answers
47k
views
attribute dependent on another field
In a model of my ASP.NET MVC application I would like validate a textbox as required only if a specific checkbox is checked.
Something like
public bool retired {get, set};
[RequiredIf("retired",...
26
votes
4
answers
24k
views
How to validate only part of the model in ASP .NET MVC?
I have a large model (large I mean model class contains a lot of fields/properties and each has at least one validation attribute (such as Required, MaxLength, MinLength etc)). Instead of creating one ...
25
votes
13
answers
60k
views
Incompatible Data Reader Exception From EF Mapped Objects
I am using Entity Framework and have updated a table and its stored procedure but I'm getting the following error when the stored procedure is called.
The data reader is incompatible with the ...
25
votes
5
answers
18k
views
How to set view model on ViewResult in request filter?
I make a MVC project and I want set Model into View from filter.
But I do not kown ,How can I do this.
the Model:
public class TestModel
{
public int ID { get; set; }
public string Name { ...
24
votes
6
answers
48k
views
Pass an entire model on form submission
I understand that I can use @Html.HiddenFor(m => m.parameter) and when the form is submitted, that parameter will be passed to the controller. My model has many properties.
Is there a shorter way ...
20
votes
4
answers
32k
views
difference between models and view models
I have been researching asp.net MVC project structure's for a new project and have a question about something is confusing me. What is the difference between models and view models? Would I be correct ...
20
votes
4
answers
23k
views
ASP.NET MVC View Model Naming Conventions
I know I will probably get a mixed opinion on this, but I was wondering if there were and "Best Practices" for model naming conventions.
I have a rather large application and I have adopted the ...
17
votes
3
answers
59k
views
pass two models to view [duplicate]
I am new to mvc and try to learn it by doing a small project with it. I have a page which is supposed to display that specific date's currencies and weather. so I should pass currencies model and ...
17
votes
4
answers
21k
views
@Html.EditorFor DateTime not displaying when set a default value to it
I'd like to set a default value to my model in Controller, But It cannot display in create page.
TestModel code:
public class TestModel
{
[DataType(DataType.DateTime), Required]
[...
16
votes
2
answers
8k
views
Passing MVC Model Data to Client-side TypeScript Code
When using MVC, I sometimes pass the server's model data to the client-side JavaScript by using Razor injected into the JavaScript, as follows:
<script type="text/javascript">
var ...
14
votes
2
answers
31k
views
How do I map checkboxes onto MVC model members?
I have an MVC view
<%@ Page Language="C#" MasterPageFile="PathToMaster" Inherits="System.Web.Mvc.ViewPage<ModelData>" %>
and I have a form with HTML markup for a set of checkboxes:
<...
14
votes
2
answers
7k
views
Display errors using Knockout JS + MVC + Server-side Model Validation?
Html form is controlled using Knockout JS and jQuery templates.
Basic jQuery validation is in use to validate fields.
Form gets serialized to JSON and submitted to MVC controller action using AJAX.
...
13
votes
4
answers
25k
views
MVC Error: The model item passed into the dictionary is null
I'm just trying to build a view but I'm getting the following error:
System.InvalidOperationException: The
model item passed into the dictionary
is null, but this dictionary requires
a non-...
12
votes
3
answers
4k
views
Ignore some fields using the helper @Html.EditorForModel()
I use helper @Html.EditorForModel() on all my views.
There is a desire that he skip two fields in my model, but only on this view, the other he must continue to display these fields as usual.
How ...
12
votes
3
answers
25k
views
ModelState.IsValid or Model.IsValid?
I'm writing a controller and unit tests for it, when I came across two ways (equally valid I think) to do something. All my models have an IsValid property, which I can check to ask the model if it's ...
12
votes
1
answer
36k
views
ASP .NET MVC - Using a enum as part of the model
(just learning MVC)
I have created a model class:
public class Employee
{
public int ID { get; set; }
[Required(ErrorMessage="TM Number is Required")]
public string ...
12
votes
2
answers
5k
views
How to prevent Razor from adding prefixes to inputs when using nested display templates?
When I use nested display templates and add input elements through the HTML helper the Razor engine adds a prefix to the fields names.
I do understand this is done to guarantee input name uniqueness ...
11
votes
2
answers
248
views
Date in model binding is being set as American format even though culture is set as British in .net core 2.1
Model binding for date in .net core 2.1 is setting date value in American format even though culture info is set as British format.
Any idea ?
Here is my startup file code snippet
var ...
10
votes
7
answers
38k
views
Disable Model Validation in Asp.Net MVC
How do I disable Model validation for a single Action in a Controller ?
Or can I do it per model by registering the model type at startup somewhere ?
I want the ModelBinder to bind to the model, but ...
10
votes
1
answer
6k
views
Custom Data Annotations ASP.NET MVC C#
I have the follwing question about MVC 2 with C#.
Here is my Model:
public class Pmjob
{
[Tooltext="Hier soll der Name eingegeben werden"]
[DisplayName("Type")]
public int Name { get; set; }...
10
votes
2
answers
4k
views
Bestpractice - Mixing View Model with Domain Model
Is it reasonable to mix view models with domain models?
So i.e. the view model object contains some domain model objects (not the other way around!)
9
votes
1
answer
15k
views
Model binding with nested child models and PartialViews in ASP.NET MVC
I have the following types and classes:
namespace MVC.Models
public class Page
{
public EditableContent Content {get; set; }
}
public class EditableContent
{
public TemplateSection ...
9
votes
3
answers
34k
views
How to use two IENumerable models in one view
I am trying to use two models in one view, but from what I understand my program just don't see any objects within models.
Here is my code.
Models:
public class Album
{
[Key]
...
9
votes
4
answers
2k
views
How do you turn off or replace the default ModelState error message in Asp.net MVC?
I have a controller action which has a nullable DateTime as one of the parameters which comes from a textbox on my form. I noticed that if the user were to type in "blah" in that textbox then I will ...
9
votes
2
answers
4k
views
ASP.NET MVC Model Binder with Global Number Formats
The default model binder is returning errors for properties that are of type double when my application is being used in countries that use different number formatting for decimals (e.g. 1.2 = 1,2). ...
9
votes
4
answers
3k
views
Binding conflict between a property named Title in my Model and View.Title in my View (in MVC)
My Model contains a property named Title, and in my Create view I set the page title using ViewBag.Title.
This creates the following problem: the form generated by Html.Editor will display the text ...
8
votes
8
answers
16k
views
Multiple models sent to a single view instance
My terminology is probably way off here but basically I'm trying to pass multiple data models to a view. To help put the question in context, take this example:
Say I was making a blog. When I log in ...
8
votes
1
answer
4k
views
MvcMailer: Taking user input in view through model and then inserting it into mail
I don't know if I am explaining this correctly, or if the solution is rather simple, so here goes:
I am using MvcMailer, but before that I set up a wizard input form which I call Quote.cshtml. Behind ...
8
votes
4
answers
8k
views
MVC3 pass @model to partial view
I have two partial views which are exactly the same, but for the @model.
@model Project.Models.X
@model Project.Models.Y
How could I pass this model type to the view so that I can use the same view ...
8
votes
1
answer
12k
views
Enum to Checkboxes in the Model C# MVC4
Now I was looking to make some validation for some checkbox fields in my model.
I want to create a unique rule that would require at least one checkbox to be true (or checked) in each category to ...
8
votes
3
answers
842
views
Creating Models in ASP.NET MVC
I'm just starting a project in ASP.Net MVC with LINQ to Entities and I was wondering if there was a nice, clean way of defining models that creates the appropriate tables in the database for me. I'm ...
8
votes
1
answer
17k
views
ASP.net MVC4: Using a different model in a partial view?
I am just learning ASP.net MVC so please bear with me if I am bad at explaining my issue.
Is it possible to use a different model in a partial view than what is being inherited in the view?
My view ...
8
votes
3
answers
2k
views
ASP.NET MVC ViewModelBuilder Suggestions
For anything but trivial view models, I use a view model builder that handles the responsibility of generating the view model object. Right now, I use constructor injection of the builders into my ...
7
votes
2
answers
12k
views
ASP.Net MVC How to display an image property of a model in a view?
I'm new to asp.net mvc, but I'm trying to do an funny app with GDI+ and I need to do some image view with asp.net mvc.
I have a Model which has an image property:
namespace DomainModel.Entities
{
...
7
votes
1
answer
7k
views
Visual Studio: The system cannot find the path specified
I am developing a website in ASP.NET MVC in Visual Studio. I'm trying to add a Database connection, but it gives me the error "The system cannot find the path specified".
Here's what I am doing:
...
7
votes
2
answers
9k
views
What's the role of the Model in MVC?
I've read a few articles about MVC but there's one thing that isn't clear to me. What is the role of the model in practical term.
Does the model represent the business object?
Or is it just a class ...
7
votes
3
answers
4k
views
ASP.NET MVC: How to handle model data that must go to every view?
So if there is some global state that every view of an MVC app will need to render ... for example: IsUserLoggedOn and UserName ... whats the appropriate way of getting that information to every view?
...