Click here to Skip to main content
15,867,964 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

This is my first query on Code Project and hope to get a good response. The scenario is like this.I have a model class Project with properties like ProjectID, Name, Client etc. Client is another model class which I have referenced in Project class.

In Create Project method in Project Controller, I am binding Clients to a dropdown ie client id and client name. On Post method,I have the following code

C#
[HttpPost]
[ActionName("Create")]
public ActionResult Create_Post()
{
    Project project = new Project();
    project.Client = new Client();          
    TryUpdateModel(project);
    if (ModelState.IsValid)//==> returns false
    {
        balProject().addeditproject(project);                
        return RedirectToAction("Index");
    }
    else
    {
        List<client> clients = balClient().getclientfordropdown();
        ViewBag.Clients = clients;                
        return View();
    }
}

When 'if' block is executed ModelState.IsValid returns false in spite of all the members being instantiated, except for client class were in justs clientid is instanciated. Is this the reason for ModelState.IsValid to be false?

I can not instantiated all the members of client class here. Any work around here?

What I have tried:

I have replaced the property Client in Project class with ClientID. However, the previous approach should also work as it used to in ASP.NET. What did I miss out on?
Posted
Updated 17-Oct-17 6:11am
v2
Comments
F-ES Sitecore 17-Oct-17 5:35am    
Look at the ModelState collection errors and it will tell you the reason the state isn't valid

https://stackoverflow.com/questions/573302/how-do-i-get-the-collection-of-model-state-errors-in-asp-net-mvc

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900