0

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 &raquo;</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.

enter image description here

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

sridharnetha
  • 2,104
  • 8
  • 35
  • 69
  • 1
    What line of code is throwing the exception (debug your code) –  May 16 '18 at 10:29
  • 2
    Where are you instantiating `MyViewModel.Con`? I don't see a `new MySqlConnectionStringBuilder()` anywhere. – CompuChip May 16 '18 at 10:29
  • 1
    A MySqlConnectionStringBuilder should not be part of a ViewModel. – bommelding May 16 '18 at 10:30
  • And as a side note, do never set the `value` attribute when using the `HtmlHelper` methods. You set the value of the property in the GET method before you pass the model to the view –  May 16 '18 at 10:31
  • @StephenMuecke even breakpoint is not hitting in my controller. I get this error message from my browser development tool (Network) window. – sridharnetha May 16 '18 at 10:32
  • Put break points in your GET method and in the view and step through it –  May 16 '18 at 10:32
  • @StephenMuecke it seems form serialized correctly. getting that error at line `$.post('/MyAction')` – sridharnetha May 16 '18 at 10:34
  • @CompuChip i tried `var model = new MySQLDB(); model.Con = new MySqlConnectionStringBuilder(); return View(model);` but the issue not resolved. – sridharnetha May 16 '18 at 10:37
  • Probably not related but you should use `$(this).serialize()` (not `serializeArray()`). You have not shown all the code in your post method, so we cannot guess what is causing the exception. –  May 16 '18 at 10:38
  • @bommelding i need to use entities of class `MySqlConnectionStringBuilder`in my razor mode, So i created another class (viewmodel) by including `MySqlConnectionStringBuilder` and other entities as well. Pleas let me know if you have another approach? – sridharnetha May 16 '18 at 10:41
  • @StephenMuecke tried `$(this).serialize()` instead `$(this).serializeArray()` but nothing difference – sridharnetha May 16 '18 at 10:43
  • I did say it was probably not related :) You still have not shown us the code that cause that exception –  May 16 '18 at 10:45
  • Is something wrong with your layout.cshtml ? – Utkarsh Bais May 29 '18 at 22:41
  • @UtkarshBais nothing wrong with my layout.cshtml. – sridharnetha May 30 '18 at 05:44
  • Try initialising the MySqlConnectionStringBuilder in your controller's constructor, and use it from there on – Utkarsh Bais Jun 06 '18 at 07:46

0 Answers0