How to pass list from view to controller - asp.net-mvc

I have problem when pass a list in an object from view to controller, the list is always null. How should I do this?
My model:
public class ImageModel
{
public int id { get; set; }
public string url { get; set; }
public bool delete { get; set; }
}
public class SchoolImages
{
public Nullable<int> schoolid { get; set; }
public IList<ImageModel> images;
}
The view:
#model SchoolAppManager.Models.SchoolImages
#using (Html.BeginForm("DeleteImages", "Images"))
{
#Html.HiddenFor(x => x.schoolid)
<table>
#for (int i = 0; i < Model.images.Count(); i += 4) {
<tr>
<td>
<img src="#Url.Content(Model.images[i].url)" width="50%" />
<br />
#Html.CheckBoxFor(x => x.images[i].delete)
#Html.HiddenFor(x => x.images[i].id)
</td>
<td>
#if (i + 1 < Model.images.Count())
{
<img src="#Url.Content(Model.images[i + 1].url)" width="50%" />
<br />
#Html.CheckBoxFor(x => x.images[i + 1].delete)
#Html.HiddenFor(x => x.images[i + 1].id)
}
</td>
<td>
#if (i + 2 < Model.images.Count())
{
<img src="#Url.Content(Model.images[i + 2].url)" width="50%" />
<br />
#Html.CheckBoxFor(x => x.images[i + 2].delete)
#Html.HiddenFor(x => x.images[i + 2].id)
}
</td>
<td>
#if (i + 3 < Model.images.Count())
{
<img src="#Url.Content(Model.images[i + 3].url)" width="50%" />
<br />
#Html.CheckBoxFor(x => x.images[i + 3].delete)
#Html.HiddenFor(x => x.images[i + 3].id)
}
</td>
</tr>
}
</table>
<input type="submit" value="delete" />
}
When I click delete, SchoolImages is passed to the controller, SchoolImages.schoolId has value in the controller, however, SchoolImages.images is null. How can I pass SchoolImages.images to controller?

Default model binder doesn't work with fields, so make images a property:
public IList<ImageModel> images { get; set; }

I guess you will have problem binding complex types to your model. In case you don't get that working. Here is the alternative way.
You have a bunch of checkboxes, possibly with the same name and different values. You could post them to a method that takes a FormCollection, ie.
public ActionResult Test(FormCollection collection)
{
string results = collection["Blanks"];
}
This would give you a comma-delimited list of values (or null, where no checkboxes are ticked).
Alternatively, if you have the possible values as an array on the server then you could give the checkboxes names according to their array values, which would mean you could do something like this:
#using (Html.BeginForm("Test","Home"))
{
#Html.CheckBox("Blanks[0]", false);
#Html.CheckBox("Blanks[1]", false);
#Html.CheckBox("Blanks[2]", false);
<input type="submit" value="Submit" />
}
giving you an array of booleans in your Test method:
public ActionResult Test(bool[] Blanks)
{ }

Try changing your model "SchoolImages"; use array instead of IList<>.

It's null because the default model binder doesn't know how to initialize it.
Change your field images to be a property and add a constructor like this:
public class SchoolImages
{
public SchoolImages()
{
images = new List<ImageModel>();
}
public Nullable<int> schoolid { get; set; }
public IList<ImageModel> images { get; set; }
}
It should work.

Related

MVC using IEnumerable<Model> with Razor is null after post to controller

My Model
public class ActivityModel
{
public int Id { get; set; }
public String Nick { get; set; }
public String TripName { get; set; }
[DisplayFormat(DataFormatString = "{0:hh\\:mm}", ApplyFormatInEditMode = true)]
public TimeSpan? FromTime { get; set; }
[DisplayFormat(DataFormatString = "{0:hh\\:mm}", ApplyFormatInEditMode = true)]
public TimeSpan? ToTime { get; set; }
public String FromPlace { get; set; }
public String ToPlace { get; set; }
public String activityType { get; set; }
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
public DateTime? Timestamp { get; set; }
public String Weather { get; set; }
public int Difficulty { get; set; }
public bool Reviewed { get; set; }
}
My View row looks like this
#model IList<ActivityModel>
#for (int i = 0; i < Model.Count; i++)
{
if (!(bool)Model[i].Reviewed)
{
<tr>
#using (Html.BeginForm("Accept", "Overview", FormMethod.Post, new { activityModel = Model }))
{
#Html.AntiForgeryToken()
<td>
#Html.DisplayFor(m => Model[i].Timestamp)
#Html.HiddenFor(m => Model[i].Timestamp)
</td>
<td>
#Html.DisplayFor(m => Model[i].Nick)
#Html.HiddenFor(m => Model[i].Nick)
</td>
<td>
#Html.DisplayFor(m => Model[i].TripName)
#Html.HiddenFor(m => Model[i].TripName)
</td>
<td>
#Html.DisplayFor(m => Model[i].FromTime)
#Html.HiddenFor(m => Model[i].FromTime)
</td>
<td>
#Html.DisplayFor(m => Model[i].FromPlace)
#Html.HiddenFor(m => Model[i].FromPlace)
</td>
<td>
#Html.DisplayFor(m => Model[i].ToTime)
#Html.HiddenFor(m => Model[i].ToTime)
</td>
<td>
#Html.DisplayFor(m => Model[i].ToPlace)
#Html.HiddenFor(m => Model[i].ToPlace)
</td>
<td>
#Html.TextBoxFor(m => Model[i].Difficulty, new { id = "Difficulty" })
<--!#Html.HiddenFor(m => Model[i].Difficulty)--!>
<div style="visibility:hidden">
#Html.HiddenFor(m => Model[i].Id)
#Html.HiddenFor(m => Model[i].Reviewed)
#Html.HiddenFor(m => Model[i].activityType)
#Html.HiddenFor(m => Model[i].Weather)
</div>
</td>
<td>
<input type="submit" name="btn_accept" value="Accept" />
</td>
}
</tr>
}
}
And my Controller is this
public ActionResult Index()
{
List<ActivityModel> activities_web = new List<ActivityModel>();
//someLogicForFilling
return View(activities_web);
}
[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult Accept(ActivityModel activityModel)
{
return RedirectToAction("Index");
}
And the problem is that in ActionResult Accept is ActivityModel always NULL empty 0 ...
I really spent whole day on it for now and I don't know where is the problem i tried almost 20 solutions and nothing worked for me..
Rendering the page and showing all values is OK but when I try to POST them like nothing is posted.
Model binding will work when the posted data matches with the property structure of the class you are using as your HttpPost action method parameter.
Your current view code is generating HTML code like this
<input name="[0].Id" type="hidden" value="101">
<input name="[1].Id" type="hidden" value="202">
Assuming you have 2 items in the collection you are passing to the view.
You can see that the name attribute value is [0].Id and [1].Id. But your Accept action method's parameter is a single ActivityModel object and the property names are like Id, FromPlace etc. For model binding to work, the names of your input and the property name should match.
You can rewrite your view code to generate the correct name attribute value for your hidden inputs. You may simply use the pure HTML code to create the hidden input and set the value from your model item or use the Html.HiddenFor helper method.
#foreach (var item in Model.Where(a => a.Reviewed == false))
{
<input type="hidden" name="FromPlace" value="#item.FromPlace" />
#Html.Hidden("ToPlace", item.ToPlace)
#Html.Hidden(nameof(ActivityModel.Id), item.Id)
}
Here is the full code (excluded some properties to save space).
#model IList<ActivityModel>
<table class="table table-striped">
#foreach (var item in Model.Where(a => a.Reviewed == false))
{
using (Html.BeginForm("Accept", "Process", FormMethod.Post))
{
#Html.AntiForgeryToken()
<tr>
<td>
#Html.DisplayFor(m => item.FromPlace)
#Html.Hidden("FromPlace", item.FromPlace)
</td>
<td>
#Html.DisplayFor(m => item.ToPlace)
#Html.Hidden("ToPlace", item.ToPlace)
</td>
<td>
#Html.Hidden(nameof(ActivityModel.Id),item.Id)
<input type="submit" name="btn_accept" value="Accept" />
</td>
</tr>
}
}
</table>
But ideally, if you are trying to update the status of a single entity /record, all you need is the Id (primary key) of that record. that means, you really do not need all the properties populated. In that case, you need the hidden input element for the Id property value. You can always query the db to get the full entity using this Id if needed. ( Never trust data coming from client)

MVC post a list of complex objects

I have a FeedbackViewModel that contains a list of questions:
public class FeedbackViewModel
{
public List<QuestionViewModel> Questions { get; set; }
}
This QuestionViewModel is an object that can be inherited by 5 different types of questions
public class QuestionViewModel
{
public string QuestionText { get; set; }
public string QuestionType { get; set; }
}
An example of one of the inheriting question types:
public class SingleQuestionViewModel : QuestionViewModel
{
public string AnswerText { get; set; }
}
In the HttpGet of the Index action in the controller I get the questions from the database and add the correct question type in list of question in the FeedbackViewModel Then I render this model in the view:
#using (Html.BeginForm())
{
//foreach (var item in Model.Questions)
for (int i = 0; i < Model.Questions.Count; i++)
{
<div class="form-group">
#Html.DisplayFor(modelItem => Model.Questions[i].QuestionText, new { #class = "control-label col-md-4" })
<div class="col-md-6">
#if (Model.Questions[i].QuestionType == "Single")
{
#Html.EditorFor(modelItem => (Model.Questions[i] as OpenDataPortal.ViewModels.SingleQuestionViewModel).AnswerText)
}
else if (Model.Questions[i].QuestionType == "Multiple")
{
#Html.TextAreaFor(modelItem => (Model.Questions[i] as OpenDataPortal.ViewModels.SingleQuestionViewModel).AnswerText)
}
else if (Model.Questions[i].QuestionType == "SingleSelection")
{
#Html.RadioButtonForSelectList(modelItem => (Model.Questions[i] as OpenDataPortal.ViewModels.SingleSelectionQuestionViewModel).SelectedAnswer,
(Model.Questions[i] as OpenDataPortal.ViewModels.SingleSelectionQuestionViewModel).SelectionAnswers)
}
else if (Model.Questions[i].QuestionType == "MultipleSelection")
{
#Html.CustomCheckBoxList((Model.Questions[i] as OpenDataPortal.ViewModels.MultipleSelectionQuestionViewModel).AvailableAnswers)
}
else if (Model.Questions[i].QuestionType == "UrlReferrer")
{
#Html.EditorFor(modelItem => (Model.Questions[i] as OpenDataPortal.ViewModels.SingleQuestionViewModel).AnswerText)
}
</div>
</div>
<br />
}
<br />
<button type="submit">Submit</button>
}
Now, I simply can't get it to post the list of questions in the model. Is it even possible to post a list of different object types?
Edit: Following is the list of data within the post that I discovered using Fiddler:
After much research I've found two solutions:
One is to write HTML that has hardcoded Id's and Names
Two is to convert your ICollection/IEnumerable to an Array or List (i.e IList something with an 'index'), and have an Array object in your BindingModel in your Controller POST Action.
Thanks to Phil Haack's (#haacked) 2008 blog post http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/
Which is still relevant to how the default ModelBinder works today for MVC.
(NB: the links in Phil's article to sample porject and extension methods are broken)
HTML snippet that inspired me:
<form method="post" action="/Home/Create">
<input type="hidden" name="products.Index" value="cold" />
<input type="text" name="products[cold].Name" value="Beer" />
<input type="text" name="products[cold].Price" value="7.32" />
<input type="hidden" name="products.Index" value="123" />
<input type="text" name="products[123].Name" value="Chips" />
<input type="text" name="products[123].Price" value="2.23" />
<input type="submit" />
</form>
Post array looks a bit like:
products.Index=cold&products[cold].Name=Beer&products[cold].Price=7.32&products.Index=123&products[123].Name=Chips&products[123].Price=2.23
Model:
public class CreditorViewModel
{
public CreditorViewModel()
{
this.Claims = new HashSet<CreditorClaimViewModel>();
}
[Key]
public int CreditorId { get; set; }
public string Comments { get; set; }
public ICollection<CreditorClaimViewModel> Claims { get; set; }
public CreditorClaimViewModel[] ClaimsArray {
get { return Claims.ToArray(); }
}
}
public class CreditorClaimViewModel
{
[Key]
public int CreditorClaimId { get; set; }
public string CreditorClaimType { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:N2}")]
public Decimal ClaimedTotalAmount { get; set; }
}
Controller GET:
public async Task<ActionResult> Edit(int id)
{
var testmodel = new CreditorViewModel
{
CreditorId = 1,
Comments = "test",
Claims = new HashSet<CreditorClaimViewModel>{
new CreditorClaimViewModel{ CreditorClaimId=1, CreditorClaimType="1", ClaimedTotalAmount=0.00M},
new CreditorClaimViewModel{ CreditorClaimId=2, CreditorClaimType="2", ClaimedTotalAmount=0.00M},
}
};
return View(model);
}
Edit.cshtml:
#Html.DisplayNameFor(m => m.Comments)
#Html.EditorFor(m => m.Comments)
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(m => Model.Claims.FirstOrDefault().CreditorClaimType)
</th>
<th>
#Html.DisplayNameFor(m => Model.Claims.FirstOrDefault().ClaimedTotalAmount)
</th>
</tr>
<!--Option One-->
#foreach (var item in Model.Claims)
{
var fieldPrefix = string.Format("{0}[{1}].", "Claims", item.CreditorClaimId);
<tr>
<td>
#Html.DisplayFor(m => item.CreditorClaimType)
</td>
<td>
#Html.TextBox(fieldPrefix + "ClaimedTotalAmount", item.ClaimedTotalAmount.ToString("F"),
new
{
#class = "text-box single-line",
data_val = "true",
data_val_number = "The field ClaimedTotalAmount must be a number.",
data_val_required = "The ClaimedTotalAmount field is required."
})
#Html.Hidden(name: "Claims.index", value: item.CreditorClaimId, htmlAttributes: null)
#Html.Hidden(name: fieldPrefix + "CreditorClaimId", value: item.CreditorClaimId, htmlAttributes: null)
</td>
</tr>
}
</table>
<!--Option Two-->
#for (var itemCnt = 0; itemCnt < Model.ClaimsArray.Count(); itemCnt++)
{
<tr>
<td></td>
<td>
#Html.TextBoxFor(m => Model.ClaimsArray[itemCnt].ClaimedTotalAmount)
#Html.HiddenFor(m => Model.ClaimsArray[itemCnt].CreditorClaimId)
</td></tr>
}
Form is processed in the Controller:
Post Model:
public class CreditorPostViewModel
{
public int CreditorId { get; set; }
public string Comments { get; set; }
public ICollection<CreditorClaimPostViewModel> Claims { get; set; }
public CreditorClaimPostViewModel[] ClaimsArray { get; set; }
}
public class CreditorClaimPostViewModel
{
public int CreditorClaimId { get; set; }
public Decimal ClaimedTotalAmount { get; set; }
}
Controller:
[HttpPost]
public ActionResult Edit(int id, CreditorPostViewModel creditorVm)
{
//...
Make sure you are rendering your view in order so that Model.Questions[i] renders in order.
For example, Model.Questions[0], Model.Questions[1], Model.Questions[2].
I noticed that if the order is not correct mvc model binder will only bind the first element.
Thanks for pointing me in the right direction with this post. I was struggling to get the syntax right for binding a non-sequential IDictionary<string, bool> object. Not sure this is 100% correct, but this Razor code worked for me:
<input type="hidden" name="MyDictionary.Index" value="ABC" />
<input type="hidden" name="MyDictionary[ABC].Key" value="ABC" />
#Html.CheckBox(name: "MyDictionary[ABC].Value", isChecked: Model.MyDictionary["ABC"], htmlAttributes: null)
If you need a checkbox, be sure to use Html.CheckBox instead of a standard HTML checkbox. The model will blow up if a value is not provided, and Html.CheckBox generates a hidden field to ensure a value is present when the checkbox is not checked.
Using Razor you can implement the for loop using a dictionary as follows without making changes to your object:
#foreach (var x in Model.Questions.Select((value,i)=>new { i, value }))
{
if (Model.Questions[x.i].QuestionType == "Single")
{
#Html.EditorFor(modelItem => (modelItem.Questions[x.i] as OpenDataPortal.ViewModels.SingleQuestionViewModel).AnswerText)
}
...
}
The collection needs to be either a List or Array for this to work.
I use this code maybe its can help
<input type="hidden" name="OffersCampaignDale[#(item.ID)].ID" value="#(item.ID)" />
#Html.Raw(Html.EditorFor(modelItem => item.NameDale, new { htmlAttributes = new { #class = "form-control" } })
.ToString().Replace("item.NameDale", "OffersCampaignDale[" + item.ID+ "].NameDale").Replace("item_NameDale", "NameDale-" + item.ID))
#Html.ValidationMessageFor(modelItem => item.NameDale, "", new { #class = "text-danger" })

Post Model back to Server

My aim is to export the clicked table, to excel (csv). This requires taking the Model on the page, and posting it to the server. But its providing difficult as its a list.
How do I post the Model in the View, to the controller.
Below is my code
View:
#model List<NameSpace.Property>
#using (Html.BeginForm("Export","Excel", FormMethod.Post))
{
<input type="submit" class="submit" value="Submit" />
}
<table .... />
Controller:
public void Export(List<Property> list)
{
}
As you did not state whether your table displays static data or dynamic data I have provided both solutions:
STATIC:
For example, if you request a url (lets say): /home/index/1. The table data is retrieved for that id (of 1) and then displayed in a table in the view.
If the information in the table does not change then you do not need to post any information back to the server. You should perform a GET request rather than a POST request to retrieve the CSV.
You would need to change the view to accept a model containing the original id that was requested and to use an action link rather than a form:
#model MyTableViewModel
#Html.ActionLink("Click here to export", "Export", "Excel", new { id = Model.Id });
<table .... />
Where the view model might be:
public class MyTableViewModel
{
public int Id { get; set; }
public List<Customer> Customers { get; set; }
}
public class Customer
{
public string Name { get; set; }
public string Street { get; set; }
public string PostCode { get; set; }
}
Then your export controller action would be:
public FileContentResult Export(int id)
{
string csv = //Retrieve the information for the provided id and convert to csv format
return File(new System.Text.UTF8Encoding().GetBytes(csv), "text/csv", "expostfilename.csv");
}
DYNAMIC:
The view model would no longer need the id so would become:
public class MyTableViewModel
{
public List<Customer> Customers { get; set; }
}
The view could be written:
#model MyTableViewModel
#using (Html.BeginForm("Export", "Home"))
{
<table>
<tbody>
#for (int i = 0; i < Model.Customers.Count; i++)
{
<tr>
<td>Name: #Html.TextBoxFor(m => m.Customers[i].Name)</td>
<td>Street: #Html.TextBoxFor(m => m.Customers[i].Street)</td>
<td>PostCode: #Html.TextBoxFor(m => m.Customers[i].PostCode)</td>
</tr>
}
</tbody>
</table>
<input type="submit" value="submit" />
}
And the controller export action:
public FileContentResult Export(MyTableViewModel vm)
{
string csv = //convert view model (vm) to csv
return File(new System.Text.UTF8Encoding().GetBytes(csv), "text/csv", "expostfilename.csv");
}
Controller Action
public void Export(List<Property> list)
{
}
Property Class
public class Property
{
public string Name { get; set; }
public string Street { get; set; }
public string Postcode { get; set; }
}
Html Table
<table id="my-table">
<tbody>
<tr>
<td>Chris</td>
<td>Awesome Street</td>
<td>DH9 4LD</td>
</tr>
<tr>
<td>Sean</td>
<td>Lame Street</td>
<td>DH8 4SR</td>
</tr>
</tbody>
</table>
Jquery
<script type="text/javascript">
var aData = {};
$('#my-table tbody tr').each(function(index, value) {
aData['list[' + index + '].Name'] = $(value).children().eq(0).text();
aData['list[' + index + '].Street'] = $(value).children().eq(1).text();
aData['list[' + index + '].Postcode'] = $(value).children().eq(2).text();
});
$.post("#Url.Action("Export")", aData, function(data) {
});
</script>
This should be enough to get you started. Hope this helps
Update
Alternatively if you did not want a Jquery specific solution, you could output your data in hidden fields
<input type="hidden" name="[0].Name" value="Chris" />
<input type="hidden" name="[0].Street" value="..." />
<input type="hidden" name="[0].Postcode" value="...." />
<input type="hidden" name="[1].Name" value="Sean" />
<input type="hidden" name="[1].Street" value="..." />
<input type="hidden" name="[1].Postcode" value="...." />
and this would get submitted in your form

When i set value of elements in editor template for grid pop up create , posting to controller as null

in my view there are two grid. when i select a row in first grid, second one is binding according to first one.
what i want to do is take common parameters from first one, used in second one create template in readonly or disabled inputs. my problem is input elements take parameter from first grid but, dont post to controller.
Controller Function
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult DonemKursSinifiOlustur([DataSourceRequest] DataSourceRequest request, DonemKursSinifi model,string DonemId, string DersId, string EgitmenId )
{
if (model != null && ModelState.IsValid)
{
Helper.Islemci.DonemKursSinifiTanimla(model);
}
return Json(new[] { model }.ToDataSourceResult(request, ModelState));
}
model.DonemId, model.DersId, model.EgitmenId and DonemId, DersId, EgitmenId come null.
EditorTemplate View for Grid Create and Update
#model Kurslar.Models.DonemKursSinifi
#using (Html.BeginForm("DonemKursSinifiOlustur","Tanim",FormMethod.Post))
{
<table>
<tr>
<td>
Lütfen Gün ve Saati Belirtiniz:
</td>
<td>
#Html.Kendo().AutoCompleteFor(m=>m.Tanim)
</td>
</tr>
<tr>
<td>
Donem :
</td>
<td>
#Html.Kendo().AutoCompleteFor(m=>m.DonemBaslangicBitis)
#Html.HiddenFor(m => m.DonemId)
</td>
</tr>
<tr>
<td>
Ders Adı:
</td>
<td>
#Html.Kendo().AutoCompleteFor(m=>m.DersAdi)
#Html.HiddenFor(m => m.DersId)
</td>
</tr>
<tr>
<td>
Eğitmen
</td>
<td>
#Html.Kendo().AutoCompleteFor(m=>m.EgitmenAdiSoyadi)
#Html.HiddenFor(m => m.DonemId)
</td>
</tr>
</table>}
First AutoCompleteFor works correctly because take input from user, not before setted.
*and my javaScript code to fill parameters to EditorTemplate *
and it works fine
var grid = $("#donemGrid").data("kendoGrid");
var rows = grid.select();
alert(rows);
try {
var donemID = grid.dataItem(rows).DonemId;
var dersID = grid.dataItem(rows).DersId;
var egitmenID = grid.dataItem(rows).EgitmenId;
var dersAdi = grid.dataItem(rows).DersAdi;
var egitmenAdiSoyadi= grid.dataItem(rows).EgitmenAdiSoyadi;
var donemBaslangicBitis = grid.dataItem(rows).DonemBaslangicBitis;
} catch (e) {
alert(e);
}
$("#DonemBaslangicBitis").data("kendoAutoComplete").value(donemBaslangicBitis);
$("#DersAdi").data("kendoAutoComplete").value(dersAdi);
$("#EgitmenAdiSoyadi").data("kendoAutoComplete").value(egitmenAdiSoyadi);
$("#DonemId").val(donemID);
$("#DersId").val(dersID);
$("#EgitmenId").val(egitmenID);
*if needed, my model *
public class DonemKursSinifi
{
[Key]
[Required]
[PersistentProperty(IsAutoIncremented = true)]
public int Id { get; set; }
[PersistentProperty]
public string Tanim { get; set; }
[PersistentProperty]
public int DonemId { get; set; }
[PersistentProperty]
public int DersId { get; set; }
[PersistentProperty]
public int EgitmenId { get; set; }
[PersistentProperty]
public int KontenjanSayisi { get; set; }
[PersistentProperty]
public int TarifeId { get; set; }
[PersistentProperty]
public int IslemNo { get; set; } // default 1
public string EgitmenAdiSoyadi { get; set; }
public string DersAdi { get; set; }
public string DonemBaslangicBitis { get; set; }
}
ok, probably you have repeated the id in the grid and also have the same name attributes in the same form to do this:
#Html.HiddenFor(m => m.DersId)
mabe you can do somethin like this:
form:
#model Kurslar.Models.DonemKursSinifi
#using (Html.BeginForm("DonemKursSinifiOlustur","Tanim", FormMethod.Post, new { id="myform"}))
{
<input type="hidden" value="" name="Tanim" />
<input type="hidden" value="" name="DonemBaslangicBitis" />
<input type="hidden" value="" name="DonemId" />
<input type="hidden" value="" name="DersAdi" />
<input type="hidden" value="" name="DersId" />
<input type="hidden" value="" name="EgitmenAdiSoyadi" />
<input type="hidden" value="" name="DonemId" />
}
table:
<table>
<tr>
<td>Lütfen Gün ve Saati Belirtiniz:</td>
<td>#Html.Kendo().AutoCompleteFor(m=>m.Tanim)</td>
</tr>
<tr>
<td>Donem :</td>
<td>#Html.Kendo().AutoCompleteFor(m=>m.DonemBaslangicBitis) #Html.HiddenFor(m => m.DonemId)</td>
</tr>
<tr>
<td>Ders Adı:</td>
<td>#Html.Kendo().AutoCompleteFor(m=>m.DersAdi) #Html.HiddenFor(m => m.DersId)</td>
</tr>
<tr>
<td>Eğitmen</td>
<td>#Html.Kendo().AutoCompleteFor(m=>m.EgitmenAdiSoyadi) #Html.HiddenFor(m => m.DonemId)</td>
</tr>
</table>
js:
var
grid = $("#donemGrid").data("kendoGrid"),
rows = grid.select(),
form = $('#myform');
form.find('input[name="DonemBaslangicBitis"]').val(grid.dataItem(rows).DonemBaslangicBitis);
form.find('input[name="DersAdi"]').val(grid.dataItem(rows).DersAdi);
form.find('input[name="EgitmenAdiSoyadi"]').val(grid.dataItem(rows).EgitmenAdiSoyadi);
form.find('input[name="DonemId"]').val(grid.dataItem(rows).DonemId);
form.find('input[name="DersId"]').val(grid.dataItem(rows).DersId);
form.find('input[name="EgitmenId"]').val(grid.dataItem(rows).EgitmenId);
form.submit();

MVC 4 Model is empty when I try to submit a form

I have two forms on one View executing two separate Action methods in one Controller.
The first form (frmRefresh) is responsible for getting data and displaying it on the form so the user can pick certain checkboxes. Once submitted, the data is returned just fine in the ViewModel and is properly displayed on the form. 11 records for the Templates and 3 records for the Guarantors are displyaed as checkboxes on the form.
The second form (frmProcess), is responsible for taking the data on the form (that came back from the first post above). The user makes selections on the screen and processes it against some logic in the Controller. I have List objects in the model and don't suppose I can use the FormCollection to process the data because of the complex objects. Basically, they are a collection of checkboxes. I really need to use the data that should be submitted in the Model because of processing in the Controller for that data.
When submitting the second form, I realize that the loanid & ddl will not be available unless I put them in a hidden field (because they are in a separate form) --- that's fine. What I'm having a great deal of difficulty in understanding is when I submit the second form (frmProcess), why doesn't the model view binder take the data from the form, put it in the model and submit it to my GeneratePDF action method.?
Number one, I really need some help in understanding why this is happening and number two, I really need a soltution which takes my model data from the form to the action method and processes it. As you can see in the Controller, at the end of the code, I'm enumerating the Templates in the ViewModel to process the data.
Please help, as I am totally stuck on this at work and they are depending on me for this. I just don't get why the model binder doesn't take the values on the form and submit it to the action method for processing. It appears I'm missing something to allow the data to get back into the Model upon submission.
Below is my pertinent code:
ViedwModel
public partial class ViewModelTemplate_Guarantors
{
public int SelectedTemplateId { get; set; }
public IEnumerable<PDFTemplate> Templates { get; set; }
public int SelectedGuarantorId { get; set; }
public IEnumerable<tGuarantor> Guarantors { get; set; }
public string LoanId { get; set; }
public string SelectedDeptText { get; set; }
public string SelectedDeptValue { get; set; }
public string LoanType { get; set; }
public bool ShowTemps { get; set; }
public string Error { get; set; }
public string ErrorT { get; set; }
public string ErrorG { get; set; }
public bool ShowGeneratePDFBtn { get; set; }
}
View
#model PDFConverterModel.ViewModels.ViewModelTemplate_Guarantors
#{
ViewBag.Title = "BHG :: PDF Generator";
}
<h2>#ViewBag.Message</h2>
<div>
<table style="width: 1000px">
<tr>
<td colspan="5">
<img alt="BHG Logo" src="~/Images/logo.gif" />
</td>
</tr>
#using (Html.BeginForm("Refresh", "Home", FormMethod.Post, new { id = "frmRefresh" })) { <tr>
<td>
#*#(Html.Kendo().NumericTextBox<int>()
.Name("txtLoanID")
.Placeholder("Enter numeric value")
)*#
#Html.LabelFor(model => model.LoanId)
#Html.TextBoxFor(model => model.LoanId)
#Html.ValidationMessageFor(model => model.LoanId)
</tr>
<tr>
<td>#Html.LabelFor(model => model.LoanType)
#Html.TextBox("SBA", "SBA")
#Html.ValidationMessageFor(model => model.LoanType)
#*#Html.TextBoxFor(model => model.LoanType)*#
</td>
<td>
<label for="ddlDept">Department:</label>
#(Html.Kendo().DropDownListFor(model => model.SelectedDeptText)
.Name("ddlDept")
.DataTextField("DepartmentName")
.DataValueField("DepartmentID")
.Events(e => e.Change("Refresh"))
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetDepartments", "Home");
});
})
)
#Html.ValidationMessageFor(model => model.SelectedDeptText)
</td>
</tr>
<tr>
<td colspan="3">
<input type="submit" id="btnRefresh" value='Refresh' />
</td>
</tr>
}
#using (Html.BeginForm("GeneratePDF", "Home", FormMethod.Post, new { id = "frmProcess" })) { if (Model.ShowGeneratePDFBtn == true)
{
if (Model.ErrorT != string.Empty)
{
<tr>
<td colspan="5">
<u><b>#Html.Label("Templates:")</b></u>
</td>
</tr>
<tr>
#foreach (var item in Model.Templates)
{
<td>
#Html.CheckBoxFor(model => item.IsChecked)
#Html.DisplayFor(model => item.TemplateName)
</td>
}
</tr>
}
else
{
Model.Error = Model.ErrorT;
}
if (Model.ErrorG != string.Empty)
{
<tr>
<td colspan="5">
<u><b>#Html.Label("Guarantors:")</b></u>
</td>
</tr>
<tr>
#foreach (var item in Model.Guarantors)
{
<td>
#Html.CheckBoxFor(model => item.isChecked)
#Html.DisplayFor(model => item.GuarantorFirstName) #Html.DisplayFor(model => item.GuarantorLastName)
</td>
}
</tr>
}
else
{
Model.Error = Model.ErrorG;
}
<tr>
<td>
<input type="submit" id="btnGeneratePDF" value='Generate PDF' />
</td>
</tr>
<tr>
<td colspan="5">
#Model.Error
</td>
</tr>
}
} </table>
</div>
<script type="text/javascript">
$('btnRefresh').on('click', '#btnRefresh', function () {
Refresh();
});
function Refresh() {
var LoanID = $("#LoanID").val();
if (LoanID != "") {
document.forms["frmTemps"].submit();
}
}
</script>
Controller
public ActionResult Index(ViewModelTemplate_Guarantors model)
{
ViewBag.Error = "";
model.ShowGeneratePDFBtn = false;
return View("Index", model);
}
// used for the first form "frmRefresh" [HttpPost] public ActionResult Refresh(ViewModelTemplate_Guarantors model) {
try
{
model.Error = string.Empty;
bool dbHasRows = db.ChkLoanFields(Convert.ToInt32(model.LoanId));
if (!dbHasRows)
{
model.ShowGeneratePDFBtn = false;
model.Error = "Details not available for this LoanId.";
return View("Index",model);
}
else
{
int TemplateCnt = 0;
int GuarantorCnt = 0;
//todo - modify 2nd & 3rd parms instead of hardcoding
ViewModelTemplate_Guarantors tg = db.SelectViewModelTemplate_Guarantors(Convert.ToInt32(model.LoanId), "All", "All", out TemplateCnt, out GuarantorCnt);
if (TemplateCnt > 0)
model.Templates = tg.Templates;
else
model.ErrorT = "Templates not available for this LoanType.";
if (GuarantorCnt > 0)
model.Guarantors = tg.Guarantors;
else
model.ErrorG = "Guarantors not available for this LoanId.";
model.ShowGeneratePDFBtn = true;
// right before the return here, the model is full of data. return View("Index", model); }
}
catch (Exception ex)
{
throw ex;
}
} [HttpPost] // when I check the data here (via submission from the "frmProcess" form, the model is completely empty, null, etc... WHY???? // i NEED the model data here to perform processing in this action method. public ActionResult GeneratePDF(ViewModelTemplate_Guarantors model) {
try
{
int FolderNo, GuarantorNum = 0;
string Folder, LoanFolder = String.Empty;
string FormId, FormName, GuarantorName = String.Empty;
int LoanId = Convert.ToInt32(model.LoanId);
LoanFolder = LoanId.ToString().PadLeft(8, '0');
//To calculate FolderId based on LoanId
if ((LoanId > 0) && (LoanId < 99000))
{
FolderNo = ((int)(LoanId / 10000) * 10000);
}
else
{
FolderNo = ((int)(LoanId / 1000) * 1000);
}
Folder = ((int)FolderNo).ToString();
Folder = Folder.PadLeft(8, '0');
//todo - 2nd parm SelectedValue of dept
List<sSRPTFundexDocCodes1_Test_Result> sSRPTFundexDocCodes1 = db.GetFormValues(Convert.ToInt32(model.LoanId), (model.SelectedDeptValue));
if (sSRPTFundexDocCodes1 != null)
{
foreach (PDFTemplate template in model.Templates) {
if (template.IsChecked == true) {
TemplateName not showing up in model after post.
This works fine... The values (the checkboxes and the corresponding names are displyaed on the form.
However, when posting the GeneratePDF button, all I see in the model is if the Checkbox is checked (which is great). After playing around with many of the following statements: (ValueFor, DisplayFor, LabelFor, EditorFor, etc), the value coming back for the Template name is blank. I need the name of the template that was checked in correspondance with the checkbox.
#Html.ValueFor(model => Model.Templates[i].TemplateName)
How can I accomplish this? Thanks ahead of time... Below is my updated code.
ViewModel public partial class ViewModelTemplate_Guarantors
{
public ViewModelTemplate_Guarantors()
{
Templates = new List<PDFTemplate>();
Guarantors = new List<tGuarantor>();
}
public int SelectedTemplateId { get; set; }
public List<PDFTemplate> Templates { get; set; }
public int SelectedGuarantorId { get; set; }
public List<tGuarantor> Guarantors { get; set; }
public string LoanId { get; set; }
public string SelectedDeptText { get; set; }
public string SelectedDeptValue { get; set; }
public string LoanType { get; set; }
public string Error { get; set; }
public string ErrorT { get; set; }
public string ErrorG { get; set; }
public bool ShowGeneratePDFBtn { get; set; }
}
Pertinet part of View:
if (Model.ShowGeneratePDFBtn == true)
{
if (Model.ErrorT == string.Empty)
{
<tr>
<td colspan="5">
<u><b>#Html.Label("Templates:")</b></u>
</td>
</tr>
<tr>
#for (int i = 0; i < Model.Templates.Count; i++)
{
<td>
#Html.CheckBoxFor(model => Model.Templates[i].IsChecked)
#Html.ValueFor(model => Model.Templates[i].TemplateName) </td>
}
</tr>
}
else
{
<tr>
<td>
<b>#Html.DisplayFor(model => Model.ErrorT)</b>
</td>
</tr>
}
if (Model.ErrorG == string.Empty)
{
<tr>
<td colspan="5">
<u><b>#Html.Label("Guarantors:")</b></u>
</td>
</tr>
<tr>
#for (int i = 0; i < Model.Guarantors.Count; i++)
{
<td>
#Html.CheckBoxFor(model => Model.Guarantors[i].isChecked)
#Html.ValueFor(model => Model.Guarantors[i].GuarantorFirstName) #Html.ValueFor(model => Model.Guarantors[i].GuarantorLastName) </td>
}
</tr>
}
else
{
<tr>
<td>
<b>#Html.DisplayFor(model => Model.ErrorG)</b>
</td>
</tr>
}
}
<tr>
<td colspan="3">
<input type="submit" name="submitbutton" id="btnRefresh" value='Refresh' />
</td>
#if (Model.ShowGeneratePDFBtn == true)
{
<td>
<input type="submit" name="submitbutton" id="btnGeneratePDF" value='Generate PDF' />
</td>
}
</tr>
<tr>
<td colspan="5">
#Model.Error
</td>
</tr>
Controller:
public ActionResult ProcessForm(string submitbutton, ViewModelTemplate_Guarantors model, FormCollection collection)
Basically, again it's working fine. When the form posts using the Generate PDF button, I get the checked value of each checkbox, but not the name of the template in the Model.
Am I missing something here???
The form before I submit is basically like below. It's the name of the checkbox (Form4) that I'm missing as a TemplateID in my Model once I get into the ActionResult.
public ActionResult ProcessForm(string submitbutton, ViewModelTemplate_Guarantors model, FormCollection collection)
checkbox (checked) Form4
#for (int i = 0; i < Model.Templates.Count; i++)
{
<td>
#Html.CheckBoxFor(model => Model.Templates[i].IsChecked)
#Html.DisplayFor(model => Model.Templates[i].TemplateName)
</td>
}
As I mentioned in my comment. The model binder cannot bind to an IEnumerable.
Your Model should look like this:
public partial class ViewModelTemplate_Guarantors
{
public ViewModelTemplate_Guarantors() {
Templates = new List<PDFTemplate>(); // These are important, the model binder
Guarantors = new List<tGuarantor>(); // will not instantiate nested classes
}
public int SelectedTemplateId { get; set; }
public List<PDFTemplate> Templates { get; set; }
public int SelectedGuarantorId { get; set; }
public List<tGuarantor> Guarantors { get; set; }
...
}
Further, your view should look like this:
...
#for(int i = 0; i < Model.Templates.Count; i++) // should really use label, not display
{
<td>
#Html.CheckBoxFor(model => Model.Templates[i].IsChecked)
#Html.DisplayFor(model => Model.Templates[i].TemplateName)
</td>
}
...
#for(int i = 0; i < Model.Guarantors.Count; i++)
{
<td>
#Html.CheckBoxFor(model => Model.Guarantors[i].isChecked)
#Html.DisplayFor(model => Model.Gurantors[i].GuarantorFirstName) #Html.DisplayFor(model => Model.Guarantors[i].GuarantorLastName)
</td>
}
...
Although a better choice would be to use an EditorTemplate and instead do this:
...
#Html.EditorFor(m => m.Templates)
...
#Html.EditorFor(m => m.Guarantors)
...
Then create a folder in ~/Views/Shared called EditorTemplates, and then create two files called Templates.cshtml and Guarantors.cshtml.
In those files you would do this:
#model PDFTemplate
<td>
#Html.CheckBoxFor(model => model.IsChecked)
#Html.DisplayFor(model => model.TemplateName)
</td>
and
#model Guarantors
<td>
#Html.CheckBoxFor(model => model.isChecked)
#Html.DisplayFor(model => model.GuarantorFirstName) #Html.DisplayFor(model => model.GuarantorLastName)
</td>
The editor templates will automatically iterate over the collection and will account for the correct naming format to make the model binder understand it's a collection.

Resources