I am using MVC with C#,
Here is my ViewModel class:
public class MyViewModel
{
public MySqlConnectionStringBuilder Con { get; set; }// Without this property everything is working fine.
public string PrName { get; set; }
public string Aky { get; set; }
public string Sky { get; set; }
}
My Razor View:
@model MySQLDB
<form class="form-horizontal myform" action="/MyAction">
@Html.TextBoxFor(mode => Model.PrName, new { @class = "form-control", @required = "required", @placeholder = "Name your backup" })
@Html.TextBoxFor(model => model.Con.Server, new { @class = "form-control", @required = "required", @placeholder = "Enter host address" })
@Html.TextBoxFor(model => model.Aky, new { @Value = "ALOJUS", @class = "form-control", @required = "required" })
@Html.TextBoxFor(model => model.Sky, new { @Value = "IJSHS9KSDFES", @class = "form-control", @required = "required" })
<button type="submit" name="ActionName" value="Connect" class="btn btn-default btn-connect">Connect »</button>
</form>
My Ajax Post:
$('.myform').on("submit", function (e) {
e.preventDefault();
$.post('/MyAction', $(this).serializeArray(), function (resp) {
alert(resp);
});
});
My Controller:
[HttpGet]
public ActionResult MyAction()
{
return View(new MyViewModel())
}
[HttpPost]
public async Task<JsonResult> MyAction(MyViewModel model)
{
var taskResult = await Task.Run(() => {
//do something
return Json(new {message="success" }, JsonRequestBehavior.AllowGet);
});
return taskResult;
}
But, getting error like Object reference not set to instance of an object
. Even it doesn't go into my Controller breakpoint.
Answers at What is a NullReferenceException, and how do I fix it? are not fully address my question, because in my scenario there are no empty or null values from the posted form, if i remove the MySqlConnectionStringBuilder
property then everything is working fine. object deserialization is not working when i use MySqlConnectionStringBuilder
property in my viewmodel class. Here MySqlConnectionStringBuilder
is class defined in the meta DLL
MySql.Data.MySqlClient