How to pass the entire ViewModel back to the controller

I have a ViewModel that contains two objects:

public class LookUpViewModel
{
    public Searchable Searchable { get; set; }
    public AddToSearchable AddToSearchable { get; set; }
}

The two contained models look something like this:

public class Searchable
{
    [Key]
    public int SearchableId { get; set; }
    public virtual ICollection<AddToSearchable> AddedData { get; set; }
}

public class AddToSearchable
{
    [Key]
    public int AddToSearchableId { get; set;}
    [Required]
    public int SearchableId { get; set; }
    [Required]
    public String Data { get; set; }
    [Required]
    public virtual Searchable Searchable { get; set; }
}

I have a view that uses mine LookUpViewModeland gets input to search SearchableId. If an object is Searchablefound, an object is created LookUpViewModeland passed to the view. Then, the editor fields for are displayed in the view AddToSearchable.Data. After submitting, I want to get LookUpViewModelpassed to the action method to process all the internal code. The only problem is that the LookUpViewModelone passed to my action method contains a null link to Searchableand a valid link to AddToSearchable.. that is, I miss half of my data.

Here is an example of my view:

@model HearingAidTrackingSystem.ViewModels.LookUpViewModel

@using (Html.BeginForm("LookUp", "Controller", "idStr", FormMethod.Post))
{
    <input type="text" name="idStr" id="idStr"/>
    <input type="submit" value="Search" />
}

@if (Model.Searchable != null && Model.AddToSearchable != null)
{
    using (Html.BeginForm("AddMyStuff", "Controller"))
    {
        Html.HiddenFor(model => model.Searchable.SearchableId);
        Html.HiddenFor(model => model.Searchable.AddedData);
        Html.HiddenFor(model => model.AddToSearchable.AddToSearchableId);
        Html.HiddenFor(model => model.AddToSearchable.SearchableId);
        Html.HiddenFor(model => model.AddToSearchable.Searchable);

        <div class="editor-field">
            @Html.EditorFor(model => model.AddToSearchable.Data)
            @Html.ValidationMessageFor(model => model.AddToSearchable.Data);
        </div>                                    

        <input type="submit" value="Submit" />
    }
}

and here are my action methods:

public ActionResult LookUp(LookUpViewModel vm)
{
    return View(vm);
}

[HttpPost]
public ActionResult LookUp(string idStr)
{
    int id = /*code to parse string to int goes here*/;

    Searchable searchable = dal.GetById(id);
    LookUpViewModel vm = new LookUpViewModel { Searchable = searchable, 
                                               AddToSearchable = new AddToSearchable() };
    //When breakpoint is set, vm contains valid references
    return View(vm);
}

[HttpPost]
public ActionResult AddMyStuff(LookUpViewModel vm)
{
    //**Problem lies here**

    //Do backend stuff
}       

. , . , . .

+5
1

:

  • , HiddenFor() Model.Searchable.

  • , Model.Searchable .

: , @Html.HiddenFor(), Html.HiddenFor();.

+5

All Articles