David Schmalzer
David Schmalzer

Reputation: 76

ASP.NET Model validation with ViewModel

I got a project where i can insert news into a database with ASP.NET MVC. What i have is a NewsModel and a NewsViewModel. The Newsmodel is declared in the NewsViewModel.

Here is my NewsViewController:

public ActionResult Index(NewsViewModel News)
    {
        //var errors = ModelState.Values.SelectMany(v => v.Errors);

            if (ModelState.IsValid)
            {
                NewsViewRepository NewsRepo = new NewsViewRepository();

                if (NewsRepo.AddNews(News))
                {
                    ViewBag.Message = "News added successfully!";
                    databaseHasChanged = true;
                }
            }


        return View(new NewsViewModel());
    }

This is my NewsViewModel:

public class NewsViewModel
{


    [Required(ErrorMessage = "First name is required.")]
    public NewsModel news { get; set; }

    public bool isvalid { get; set; } 

    private List<SelectListItem> languageItems= new List<SelectListItem>();


    [Required(ErrorMessage = "Language items requierd")]
    public List<SelectListItem> LanguageItems{get;set;}

    [Required(ErrorMessage = "convetion  requierd")]
    private List<SelectListItem> conventionItems = new List<SelectListItem>();

    [Required(ErrorMessage = "convetion  requierd")]
    public List<SelectListItem> ConventionItems {get; set;}


    public int selectedIndex = 1;
}

So the problem i have is that ModelState.IsValid is always false if i don't initalize the NewsModel in the NewsViewModel. If i initialize it it is always true and I dont get to the create view. It ends up in an error. I tried to add a boolean value to the NewsViewModel to set it to true or false and check it with that, but i cant change the value of the boolean in the View. I tried it like this:

@{ Model.isvalid = true }

So is there a way to initalize the Newsmodel in the Razor view so that the modelstate becomes valid? Or do i miss something completly here?

Info: In the previous version of my project i had no viewmodel. Only the newsmodel and there are only strings and integers in it. It worked with them.. the only difference now is that i have this NewsModel in my NewsViewModel class.

I hope somebody can help me. Didn't find any solution to it yet.

Upvotes: 1

Views: 114

Answers (2)

David Schmalzer
David Schmalzer

Reputation: 76

View models should never contain data models. Delete it and replace it with the properties of NewsModel that you want to display/edit in the view. And your [Required] attributes on your SelectLists makes no sense and would always cause ModelState to be invalid (you do not post back SelectList's –

Upvotes: 0

Anupam Singh
Anupam Singh

Reputation: 1166

You can lookup into invalid properties of modelstate using

foreach (ModelState modelState in ViewData.ModelState.Values) {
    foreach (ModelError error in modelState.Errors) {
        // errors here 
    }
}

Upvotes: 2

Related Questions