I am not allowed to post the whole code, so please help with what you can from provided snippets....
I have a View with a modal dialog with few fields and submit button that has a Click event handler attached to it. That click handler looks like this:
Modal Submit/Click Handler
var gosti = [];
$("#modalDodajGosta").on("click","#btnDodajGosta",function(){
var $modalForm = ($("#modalDodajGosta form"));
if($modalForm.valid())
{
var gost = getFormData2Object($modalForm);
if(gost != undefined)
{
gosti.push({
Id : parseInt(gost.Id),
ImePrezime : gost.ImePrezime,
DrzavaId : parseInt(gost.DrzavaId),
Email : gost.Email,
Telefon : gost.Telefon
});
}
$.ajax({
url:"@Url.Action("GenerisiListuGostiju", "Gosti")",
data:$.toJSON(gosti),
contentType: "application/json; charset=utf-8",
dataType: "html",
type:"post",
success: function(data)
{
console.log("Success!!!");
alert(data);
},
error:function(){ console.log("Connection Error!!!");}
});
}
This should send array of objects and return html (partial view). Next is...
Controller Action
[HttpPost]
public ActionResult GenerisiListuGostiju(List<GostiStavka> gosti)
{
return PartialView("~/Views/Rezervacije/Partials/_GostDisplayTemplate.cshtml", gosti);
}
At this point everything works fine, sent array is recognized as a list of ViewModels(or DTOs whatever you want to call them) but returning a PartialView is returned as nothing with current ajax settings or 0 if "dataType" is set to "json".... so no html - no partial view. Partial View looks like this ...
Partial View
@model IEnumerable<ProjectName.ViewModels.GostiStavka>
<ul id="lista-gostiju" class="list-group">
@Html.EditoFor(m => m.GostiStavka)
</ul>
Update
Editor Template
@model ProjectName.ViewModels.GostiStavka
<li class="gost-item">
<a href="#" class="btn btn-default btn-sm item-btn-left remove-item"><i class="fa fa-remove"></i></a>
<a href="#" class="btn btn-default btn-sm item-btn-right edit-item"><i class="fa fa-pencil"></i></a>
<div class="col-xs-5">@Model.ImePrezime</div>
<div class="col-xs-7">
<a href="mailto:@Model.Email" class="">@Model.Email</a>
</div>
<div class="clearfix"></div>
<div class="form-items">
@Html.HiddenFor(m => m.Id, new { @class="force-val" })
@Html.HiddenFor(m => m.ImePrezime, new { @class = "force-val" })
@Html.HiddenFor(m => m.DrzavaId, new { @class = "force-val" })
@Html.HiddenFor(m => m.Email, new { @class = "force-val" })
@Html.HiddenFor(m => m.Telefon, new { @class = "force-val" })
</div>
<div class="field-validation-error">
@Html.ValidationMessageFor(m => m.Id)
@Html.ValidationMessageFor(m => m.ImePrezime)
@Html.ValidationMessageFor(m => m.DrzavaId)
@Html.ValidationMessageFor(m => m.Email)
@Html.ValidationMessageFor(m => m.Telefon)
</div>
EditorFor is a custom EditorTemplate that is tested by directly invoking it and sending appropriate list of objects and the response was as intended. I need this editor template because I want to use its features of proper binding of input elements to model eg. 'name="GostiStavka[0].Id"'.
I tried to return json too but I need something to convert PartialView to string. Found couple of solutions here on stack overflow but at best they returned a string like "0\r\n\r\n" as if they couldn't parse the editor and returned only row/line escape characters....
My previous solution that worked involves managing these names trough "javascript" but that seemed like inventing the wheel again because MVC5 Editor Templates can do that for you.
Any ideas on making this work are appreciated. Also, new ideas to to dynamically add items to list of a certain type to ViewModel on submit without adding them to DB beforehand is also good.(sorry, not a native English speaker)
Success
` - does that work? If so, then it'll be an issue with the partial and/or the data passed to it. When your Action is hit, does `gosti` contain populated models? (not just a list of models, but do those models have values assigned correctly?) – freedomn-m Feb 08 '17 at 13:32tags and it worked. The returned html was
– Dino Feb 08 '17 at 14:17tags indeed so there is something wrong with my EditorTemplate. So I tried to iterate over that IEnumerable items but it triggered the default Editor and not my Template which is not what i want. So I will now add example of editor template that I made because clearly the problem is somewhere in there