1
votes

View Model looks like this:

public class AsmenysInfoViewModel2
{
    public asmenys_info Asmenys_info { get; set; }
    public List<miestai> Miestai { get; set; }
    public string Test { get; set; }
}

And there are two actions. Get and Post.

    public ActionResult Index(long? id)
    {

        var model = new AsmenysInfoViewModel2();
        model.Test = "Test";
        model.Asmenys_info = BllFactory.DalFactory.AsmenysInfoDal.GetById(id.Value);
        return View(model);
    }
    [HttpPost]
    public ActionResult Index(AsmenysInfoViewModel2 asmenys_info)
    {
        var model = asmenys_info;
        return View(model);
    }

And my view looks like this:

@model MODELS.AsmenysInfoViewModel2
@{
    ViewBag.Title = "Index";
}
@using (Html.BeginForm("Index", "AsmenysInfo", FormMethod.Post))
{
    @Html.ValidationSummary()
    @Html.TextBoxFor(m => m.Asmenys_info.adresas)       

    <input type="submit" value="Išsaugoti" />
}

Doesn't matter if I use EditorFor or TextBoxFor - result is same. My model property "Asmenys_info" on posting is always null. If my class AsmenysInfoViewModel2 would not contain asmenys_info type property and would contain only "string, int etc" (no strongly typed) - it would work. My question is :

How to post View Model which has strongly typed property which on posting would not be null?

2
try changing public ActionResult Index(asmenys_info asmenys_info) to public ActionResult Index(AsmenysInfoViewModel2 asmenys_info) - Shashank Sood
Same problem. I posted here wrong code. It was like you said. I edited my post to correct code which cause me problem. I think the problem is that on posting there is no instance to asmenys_info class, so it cant set m.Asmenys_info.adresas property. But I don't know how to solve it. - user2179088
Based on my experience,Complex Model form post is not possible.Though u can do it by building a json object in jquery and then posting the form. - Shashank Sood
Please update asmenys_info class. - Ali Soltani
I guess where the problem but I need to asmenys_info class that used in AsmenysInfoViewModel2 - Ali Soltani

2 Answers

2
votes

Your model has a property named Asmenys_info and the parameter in your POST method is also named asmenys_info. Internally the DefaultModelBinder reads the values of the form data which includes a value for Asmenys_info and attempts to set property Asmenys_info to that value but it fails because there is no conversion from a string to a complex object.

Change the name of the parameter to anything other than a name of a property in your model and it will bind fine, for example

[HttpPost]
public ActionResult Index(AsmenysInfoViewModel2 model)
1
votes

Change the below line with another object name

 public ActionResult Index(AsmenysInfoViewModel2 asmenys_info)

in above method use any other name of object instead of asmenys_info.

because while mvc framework map your model with object there is confution in asmenys_info and Asmenys_info property of AsmenysInfoViewModel2 class.