I'm using ASP.NET MVC C# with HTML CSS jQuery KnockoutJs frontend.
I have a modal contact form on my HTML page. The idea is that if I create a new contact the modal form pops up with blank values including a blank hidden id field.
If I edit a contact then the modal form pops up with filled out fields including that hidden id field.
In my controller I intend to do this:
public JsonResult Contact(string values)
{
var contact = JsonConvert.DeserializeObject<contact>(values);
if (contact.Id.Equals(Guid.Empty))
{
// create a new contact in the database
} else
{
// update an existing one
}
}
However, I get an error saying that can't convert "" to type Guid
How do you get around this with NewtonSoft Json, i've looked here at the Custom JsonConverter and it seems to be along the right lines, however I'm not sure where to go with this.
A custom converter would look like this, but I feel like it's a bit overkill for something so trivial.
Usage:
Alternatively:
EDIT
I presume that your JSON looks a lot like this:
The problem would probably be fixed if you can do this instead: