How can I add a new object item to a Main Views model that has been created in a different model?
I am trying to add a new item to a list which is contained in my main view model.
Before adding this item, I need info about the properties for this item, so I use a view strongly tied to a model which contains necessary input fields. I have made the model for this view a wrapper style which contains two objects. The main view’s model as well as a model for the specific item.
MODEL
public class MDTCompletedJobM : BaseM
{
...
public MDTPartMList PartsList { get; set; }
...
}
public class MDTAddPartM
{
public MDTPartM Part { get; set; }
public MDTCompletedJobM CompletedJobM { get; set; }
public List<string> PartTypes { get; set; }
public MDTAddPartM()
{
Part = new MDTPartM();
PartTypes = (List<string>)ConfigurationManager.GetSection("MTDataPartTypeList");
}
}
so when the view is called from my controller, I assign my mains views model to this model and pass it in. From the “Add Part” view, I then use an Ajax post call to actually add the part to this List (CompletedJobM.PartsList).
This works fine except I now don’t know how to get my main view to be aware in it’s model that there is an extra Item in it’s list. This new item is not “stored” anywhere after it’s been added to the model list, so I can’t reload the main view model. My Ajax post is:
$.ajax({
type: "POST",
cache: false,
url: '@Url.Action("AddPart", "Jobs")', //$(this).attr("action"),
data: $.toJSON(result),
contentType: 'application/json',
success: function (data) {
$("#progressdiv").html();
var isValid = $(data).find(".validation-summary-errors").length == 0;
if (isValid) {
var pwindow = $("#Window").data("tWindow");
pwindow.close();
$('.page').html(data); <- what to do here?
}
else {
$("#resultDiv").html(data);
}
},
beforeSend: function () {
$("#progressdiv").html('<image src=@Url.Content("~/Content/Images/ajax-loader.gif") alt="Loading, please wait" />');
},
error: function (xhtr, e) {
$("#progressdiv").html();
alert(xhtr.responseText);
}
});
Currently (since I’m returning a View) I tried just assigning the returned HTML to a tag of the main view, but that caused issues with overlapping displays and further processing issues with button clicks and so forth.
My question is at the top line? Maybe I’ve gone about it all wrong?
Thanks
EDIT
Ok, let me rephrase this question slightly. Ignore my actual code. What principles or general concept would someone use to update a model of one View (in this case a list) with the model from a different view (entry form), such that when refreshing the Main View it contains the information from the 2nd view bearing in mind that after the second view has been updated/posted, it’s not written to a DB or anywhere. It’s effectively in memory.
Thanks
What I ended up doing was storing my Model in the Session object and retrieved it and used that while re-rendering the page.