I have got a problem to get back a parameter from my view to my controller with MVC4.
This is my model’s classes (sorry for all this code, I wanted to post an image at first but I’m too newbie to be allowed to to that):
public class Form
{
public Form()
{
this.Rows = new List<Row>();
}
public List<Row> Rows { get; set; }
}
public abstract class Row
{
protected Row()
{
this.Label = string.Empty;
this.Type = string.Empty;
}
public string Label { get; set; }
public string Type { get; set; }
}
public class SimpleRow : Row
{
public SimpleRow()
{
this.Value = string.Empty;
}
public string Value { get; set; }
}
public class CheckRow : Row
{
public CheckRow()
{
this.CheckedItems = new List<CheckedItem>();
this.Id = 0;
}
public List<CheckedItem> CheckedItems { get; set; }
public int Id { get; set; }
}
public class CheckedItem
{
public CheckedItem()
{
this.Title = string.Empty;
this.Checked = false;
}
public string Title { get; set; }
public bool Checked { get; set; }
}
I managed to build my view from an input xml file which is the serialization of my model.
But my problem is, when I change some value in my view and push the save button, i get back an empty parameter in my controller function.
The controller :
public class FormController : Controller
{
// GET: /Form/
#region Public Methods and Operators
[Authorize]
public ActionResult Index(HttpPostedFileBase file)
{
this.ViewBag.Title = "Formulaire Collaborateur";
if (file != null && file.ContentLength > 0)
{
return this.View(SerialisationHelper.DeserializeFromStream<Form>(file.InputStream));
}
return this.View();
}
[HttpPost]
[Authorize]
public ActionResult EmailForm(Form updatedForm)
{
Form f = updatedForm; // Empty instance of Form
return this.View("Index");
}
#endregion
}
And my view :
@model Form
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm("EmailForm", "Form", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<fieldset>
@foreach (var row in Model.Rows)
{
@Html.Partial("Row", row)
}
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<br />
}
This view calls the other view associated to my model classes.
If you need more code I will post it.
And, please, excuse my english, I’m not a native speaker.
Florent
It’s not very clear from your code what are you doing in
@Html.Partial("Row", row)But anyway you should make a EditorTemplate for thisRowtype then just use it :