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
[HttpPost]
[ActionName("Create")]
public ActionResult Create_Post()
{
Project project = new Project();
project.Client = new Client();
TryUpdateModel(project);
if (ModelState.IsValid)
{
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?