Validation multiple text fields MVC 3 - asp.net-mvc-3

I want to do the validation of multiple text fields
Values ​​must be between 0 and #ViewBag.MaxNote
In the view I have the following:
#model IList<RolesMVC3.Models.STUDENTS>
<P>Please enter a value between 0 and : #ViewBag.MaxNote<P>
#using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" }))
{
#Html.ValidationSummary(true)
#for (var i = 0; i < Model.Count(); i++)
{
#Html.TextBox("txt")
}
<input type="submit" value="type="submit" class="btn btn-primary" />
}
I have to validate the field depending on the value of "#ViewBag.MaxNote"
How I can do this?

Related

Bind multiple Dropdowns and retrieve user selction at postback

I'm working on an MVC Core form where a requester has to define some approvers for his application. When preparing the model for the Get request, I first get the roles for the approvers. Currently, there are always four roles returned:
Category Head
Governance Head
Concessions VP
Commercial EVP
And here is the HttpGet:
[HttpGet]
public async Task<IActionResult> Create()
{
// omitted for brevity...
// Get the SystemRole models (4 models will be returned)
model.ApprovingRoles = (await serviceLookup.GetAllRolesAsync(ct)).ToList();
}
The SystemRoleModel is simply:
public class SystemRoleModel
{
public int Id { get; set; }
public string Name { get; set; }
public bool Active { get; set; }
}
The view is composed of EditorTemplate as follows:
Create.cshtml -> LetterEditor.cshtml -> LetterAttachmentEditor.cshtml
Create.cshtml:
#model LetterModel
#{
ViewData["Title"] = "Create RL";
}
#Html.EditorFor(m => m, "LetterEditor", new { ShowApprovers = "1", ShowAttachments = "1", ShowButtons = "1" } )
LetterEditor.cshtml:
#model LetterModel
...
<div class="panel-body">
#await Html.PartialAsync("EditorTemplates/LetterAttachmentEditor", new LetterAttachmentUploadViewModel { IsBusy = false, LetterGuid = Model.IdCode.ToString() })
</div>
...
And finally, LetterAttachmentEditor.cshtml:
#model IList<SystemRoleModel>
#for (var i = 0; i < Model.Count; i++)
{
var index = i;
var title = Model[index].Name;
<div class="row">
<div class="col-lg-2 mt-3">
#Html.Label("LetterApprover[" + index + "]", title, new { #class = "control-label" })
</div>
<div class="col-lg-4">
#(Html.Kendo().DropDownList().Name("LetterApprover[" + index + "]")
.DataValueField(nameof(SystemUserModel.Id))
.DataTextField(nameof(SystemUserModel.EmployeeName))
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetUsersByRoleId", "Api", new { roleId = Model[index].Id });
}).ServerFiltering(true);
})
)
</div>
<div class="col-lg-6">
<span asp-validation="" class="text-danger"></span>
#Html.ValidationMessage("LetterApprover[" + index + "]", $"An approver as a {title} is required", new { #class = "text-danger" })
</div>
</div>
}
Also, LetterModel.cs:
public class LetterModel
{
public LetterModel()
{
Approvers = new List<LetterApproverModel>();
}
// omitted for brevity...
public IList<SystemRoleModel> ApprovingRoles { get; set; } = new List<SystemRoleModel>();
}
Now, with that all out of the way, here is the final rendered dropdown (minus the kendo fluff):
<input id="ApprovingRoles_LetterApprover_0_" name="ApprovingRoles.LetterApprover[0]" required="required" type="text" validationmessage="..." data-role="dropdownlist">
<input id="ApprovingRoles_LetterApprover_1_" name="ApprovingRoles.LetterApprover[1]" required="required" type="text" validationmessage="..." data-role="dropdownlist">
<input id="ApprovingRoles_LetterApprover_2_" name="ApprovingRoles.LetterApprover[2]" required="required" type="text" validationmessage="..." data-role="dropdownlist">
<input id="ApprovingRoles_LetterApprover_3_" name="ApprovingRoles.LetterApprover[3]" required="required" type="text" validationmessage="..." data-role="dropdownlist">
If the user submits this form, I need to receive a list of selected IDs from this array of dropdowns. I followed an anti-pattern, so I'm hoping the MVC binding will do its magic here. I just need to figure out the name of the model property that I should add of type List<string>.
How about try to change the name into name="LetterApprover[0]" and name="LetterApprover[1]" and name="LetterApprover[2]" and name="LetterApprover[3]" .
Then you could bind to List<string> LetterApprover
Update
Name is auto-appended by MVC due to sub-editor
How about add js codes to change the input name when you submit the form?
I try it like below, I first add class="form-control" to dropdownlist, add id="save" to button, then:
<script>
var items = document.getElementsByClassName("form-control");
$('#save').click(function () {
for (var i = 0; i < items.length; i++)
{
items[i].setAttribute("name", "LetterApprover")
}
});
</script>
Then bind to List<string> LetterApprover.
I was able to bind the selected values to a model's property upon submission by modifying the prefix added by the MVC engine:
#using DACRL.Domain.Models.BusinessObjects
#model IList<DACRL.Domain.Models.BusinessObjects.SystemRoleModel>
#{
ViewData.TemplateInfo.HtmlFieldPrefix = "";
}
#for (var i = 0; i < Model.Count; i++)
{
var index = i;
var name = "SelectedApprover[" + index + "]";
var title = Model[index].Name;
<div class="row">
<div class="col-lg-2 mt-2">
#Html.Label(name, title, new { #class = "control-label" })
</div>
<div class="col-lg-4">
#(Html.Kendo().DropDownList().Name(name)
.Size(ComponentSize.Medium).Rounded(Rounded.Medium).FillMode(FillMode.Outline)
.HtmlAttributes(new { style = "width: 100%" })
.DataValueField(nameof(SystemUserModel.Identifier))
.DataTextField(nameof(SystemUserModel.EmployeeName))
.OptionLabel("Select " + title).Filter(FilterType.Contains)
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetUsersByRoleId", "Api", new { roleId = Model[index].Id, sequence = index + 1 });
}).ServerFiltering(true);
})
.Height(500))
</div>
<div class="col-lg-6">
<span asp-validation="" class="text-danger"></span>
#Html.ValidationMessage(name, $"An approver as a {title} is required", new { #class = "text-danger mt-2" })
</div>
</div>
}
The line ViewData.TemplateInfo.HtmlFieldPrefix = ""; allowed me to control the naming and the binding started workinfg

MVC 4 Updating a partial view from another partial view using Ajax.BeginForm()

I have a comment section set up on one of my pages. The parent view has a partial view which shows the comments for that ID and gives the option to display another partial view to post a comment. When someone post a comment I want the first partial view within the parent to refresh displaying the new comment.
Currently when you click Post Comment, the AddComment method is called and added to the database. I get an error saying that I am passing the wrong type of model to the view. It seems to be trying to pass the return value to my AddComment partial view instead of injecting it into Partent View Div.
Parent View
#model QIEducationWebApp.Models.Course
#{
ViewBag.Title = "Course Details";
}
<h1 class="page-header">#ViewBag.Title</h1>
Javascript is here
.
.
.
<table class="table">
DETAILS HERE
</table>
<ul id="view-options">
<li>#Html.ActionLink("Back to Courses", "Index", "Course")</li>
</ul>
<input type="button" id="View" class="ShowComment" value="Show Comments"/>
<div id="CommentSection"/>
Partial View to view comments
Javascript is here
.
.
.
<div class="CommentSection">
#foreach (var item in Model)
{
<div class="Comment">
<div class="CommentText">
#Html.DisplayFor(modelItem => item.CommentText)
</div>
<div class="CommentSep">
<span class="Commenter">#Html.DisplayFor(modelItem => item.UserName)</span> - <span class="CommentDate">#Html.DisplayFor(modelItem => item.CommentDate)</span>
</div>
</div>
}
<input type="button" id="Post" class="AddComment" value="Add a Comment"/>
<br />
<br />
</div>
<div id="AddComment" />
<br />
<br />
Page #(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of #Model.PageCount
#Html.PagedListPager(Model, page => Url.Action("ViewComments",
new { courseID = #ViewBag.courseID, page }),
PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(
new PagedListRenderOptions { MaximumPageNumbersToDisplay = 5, DisplayLinkToFirstPage = PagedListDisplayMode.IfNeeded,
DisplayLinkToLastPage = PagedListDisplayMode.IfNeeded },
new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "CommentSection" }))
Method behind the is partial view
public PartialViewResult ViewComments(int courseID, int? page = 1)
{
ViewBag.courseID = courseID;
var coursecomments = db.CourseComments.Where(cc => cc.CourseID == courseID);
int pageSize = 10;
int pageNumber = (page ?? 1);
return PartialView(coursecomments.OrderByDescending(cc => cc.CommentDate).ToPagedList(pageNumber, pageSize));
}
Partial to Post Comment
Javascript is here
.
.
.
#using (Ajax.BeginForm("AddComment", "CourseComment", new { courseID = #ViewBag.courseID, userName = #User.Identity.Name },
new AjaxOptions { UpdateTargetId = "CommentSection" }))
{
#Html.ValidationSummary(true)
<div class="NewComment">
<div class="editor-field">
#Html.TextAreaFor(model => model.CommentText, new { maxLength = 500 })
#Html.ValidationMessageFor(model => model.CommentText)
</div>
<input type="submit" class="PostComment" value="Post Comment" />
<div id="Counter" class="CommentCounter"/>
</div>
}
Controller method linked to the Post Comment Ajax.BeginForm()
public PartialViewResult AddComment(CourseComment coursecomment, int courseID, String userName)
{
coursecomment.CommentDate = System.DateTime.Now;
coursecomment.CourseID = courseID;
coursecomment.UserName = userName;
if (ModelState.IsValid)
{
db.CourseComments.AddObject(coursecomment);
db.SaveChanges();
}
ViewBag.courseID = courseID;
return ViewComments(courseID);
}
Adding pictures
Details
After selecting View Comments button
After selecting Add Comment
After Posting the the comment I want the list of Comments to refresh displaying the newly added Comment. Like So
For now I have it changed. I wanted to the comments section to be hidden until the show comments was clicked. Then after posting a comment on the comments section was refreshed, but I couldn't get that to work. So just reloading the whole page will refresh the comments section, but make it hidden at that time. I made it so that the comments section shows by default without the option to hide it. So unless anyone can figure out a way to get it to work how I wanted, this works for now.

form post not working due to #HiddenFor in mvc3

for some reason, my form post does not work when i have the hidden field in mvc3 razor. It works fine if i remove the hidden field but i need that field.
Below are my ProductsController post method and razor view
#model CCSPurchasing_MVC.Models.AddNewProductModel
#using CCSPurchasing_MVC.Models
#using (Html.BeginForm("editImage", "Products", FormMethod.Post))
{
#Html.ValidationSummary(true)
#Html.HiddenFor(m => m.ImadeId)
<div class="editor-field">
<p style="margin-left: 300px; margin-right: 20px;">
#Html.LabelFor(m => m.ImageFile)
<input type="file" name="file" id="file" data-val="true" data-val-required="Product Image is required"/>
</p>
</div>
<input type="submit" value="Edit" />
}
[HttpPost]
public ActionResult editImage(AddNewProductModel newP, HttpPostedFileBase file)
{
db = new DBConnection();
if (file != null && file.ContentLength > 0)
{
newP.ImageName = Path.GetFileName(file.FileName);
newP.ImageType = file.ContentType;
newP.ImageSize = file.ContentLength;
newP.ImageFile = new byte[file.ContentLength];
file.InputStream.Read(newP.ImageFile, 0, file.ContentLength);
newP.ImageHeight = 200;
newP.ImageWidth = 200;
}
string result = db.editImage(newP);
return View("editImageUpdate");
}
Just make your form tag like this and I am sure it will work for you also as it worked for me when I tested your code:
#using (Html.BeginForm("EditImage", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
}
You need to add enctype = "multipart/form-data" also in your code if you want to submit the file with your fileupload control.

I have #Ajax.BeginForm in each row of Table, When I make post request using that form then Model is null except first row

I have #Ajax.BeginForm in each row of Table, When I make post request using that form then Model is null except first row.
Only first form is working, second and so on forms are not working.... I have used Form ID unique for all.
Probably, because of I am using IList and for loops,
AND first form has controls name like [0].lstMatches[4].match_set_name
and second,third,.... form has control name like [n].lstMatches[4].match_set_name
and IList can provides the model only for First one, [0].lstMatches[4].match_set_name
Here is my View structure
#model IList<QIOnlineMVC3.Models.TravelAdsRulesMatchsGroups>
#{
ViewBag.Title = "Business Rule Travel Ads";
ViewBag.Layout = "_Layout2";
}
<table id="PageSortTable" class="travelads_business_rules" cellspacing="0">
<tr></tr>
<tbody>
#for (var i = 0; i < Model.Count; i++)
{
<tr class="minimised" id='trMinimised#(Model[i].id)'>
<td colspan="13">
#using (Ajax.BeginForm("_SaveGroupMatchs", null, new AjaxOptions { OnSuccess = "onSuccess", UpdateTargetId = "msgResult", LoadingElementId = "msgLoading", HttpMethod = "Post" }, new { #id = "SaveForm" + Model[i].id.ToString() }))
{
<div class="detail">
<div class="table_header">
<span class="col_a">Group</span> <span class="col_b">Group Scope</span> <span class="col_c">
Seq</span> <span class="col_d"> </span> <span class="col_e">ID</span> <span
class="col_f">Match Location</span> <span class="col_g">Target Field</span>
<span class="col_h">Match Type</span> <span class="col_i">Set Name</span>
</div>
<!-- Loop for matches -->
<!-- start GROUP -->
<div class="group" id="Group_rule_#(Model[i].id)">
#Html.HiddenFor(model => #Model[i].id)
#Html.HiddenFor(model => #Model[i].ad_rule_name)
#Html.HiddenFor(model => #Model[i].ad_rule_desc)
#Html.HiddenFor(model => #Model[i].business)
#Html.HiddenFor(model => #Model[i].ad_region)
#Html.HiddenFor(model => #Model[i].ad_form)
#Html.HiddenFor(model => #Model[i].ad_company)
#Html.HiddenFor(model => #Model[i].ad_team)
#Html.HiddenFor(model => #Model[i].ad_language)
#Html.HiddenFor(model => #Model[i].ad_media_id)
#Html.HiddenFor(model => #Model[i].ad_insert_loc)
#Html.HiddenFor(model => #Model[i].ad_priority)
#Html.HiddenFor(model => #Model[i].access)
#Html.HiddenFor(model => #Model[i].deployment)
#for (var j = 0; j < Model[i].lstMatches.Count; j++)
{
<div id="rule#(Model[i].id)_divRuleGroup_#(Model[i].lstMatches[j].match_group_num)">
#Html.HiddenFor(model => Model[i].lstMatches[j].match_rule_id)
#Html.HiddenFor(model => Model[i].lstMatches[j].match_id)
#Html.HiddenFor(model => Model[i].lstMatches[j].match_group_num)
#Html.HiddenFor(model => Model[i].lstMatches[j].match_rule_type)
#Html.HiddenFor(model => Model[i].lstMatches[j].created)
#Html.HiddenFor(model => Model[i].lstMatches[j].access)
<header id="header_rule_Group"><span class="col_a">
#if (Model[i].lstMatches[j].Position == "First")
{
#(Model[i].lstMatches[j].match_group_num)
} </span>
<span class="col_b">
#if (Model[i].lstMatches[j].Position == "First")
{
#Html.DropDownListFor(model => Model[i].lstMatches[j].match_group_scope, new SelectList((List<QIOnlineMVC3.Helpers.clsGroupScope>)QIOnlineMVC3.Helpers.General.GetALLGroupScope(), "GroupScope", "GroupScope", Model[i].lstMatches[j].match_group_scope))
}
else
{
#Html.HiddenFor(model => Model[i].lstMatches[j].match_group_scope)
}
</span></header>
<div class="match">
<span class="col_c">#(Model[i].lstMatches[j].match_group_seq) #Html.HiddenFor(model => Model[i].lstMatches[j].match_group_seq) </span>
<span class="col_d">
<a href="#(Url.Action("DeleteMatch", new { id = Model[i].lstMatches[j].match_id }))">
<img alt="Delete" src="../../Content/Images/delete-icon.png" width="17" height="17" border="0" title="delete" /></a>
</span><span class="col_e">#(Model[i].lstMatches[j].match_id)
</span><span class="col_f">
#Html.DropDownListFor(model => Model[i].lstMatches[j].match_loc, new SelectList((List<QIOnlineMVC3.Helpers.clsMatchLocations>)QIOnlineMVC3.Helpers.General.GetALLMatchLocations(), "MatchLocations", "MatchLocations", Model[i].lstMatches[j].match_loc))
</span><span class="col_g">
#Html.DropDownListFor(model => Model[i].lstMatches[j].match_target_field, new SelectList((List<QIOnlineMVC3.Helpers.clsTargetField>)QIOnlineMVC3.Helpers.General.GetALTargetField(), "TargetField", "TargetField", Model[i].lstMatches[j].match_target_field))
</span><span class="col_h">
#Html.DropDownListFor(model => Model[i].lstMatches[j].match_type, new SelectList((List<QIOnlineMVC3.Helpers.clsMatchType>)QIOnlineMVC3.Helpers.General.GetALLMatchType(), "MatchTypeID", "MatchType", Model[i].lstMatches[j].match_type))
</span><span class="col_i">
#Html.TextBoxFor(model => Model[i].lstMatches[j].match_set_name, new { #value = Model[i].lstMatches[j].match_set_name, #class = "clsauto" })
</span></div>
</div>
if (Model[i].lstMatches[j].Last == "Last")
{
<div id="Group_rule_#(Model[i].id)_#(Model[i].lstMatches[j].match_group_num)"></div>
<!-- end MATCH -->
<div class="new">
#Ajax.ImageWithTextActionLink("<img src=\"../../Content/Images/new-icon.png\" width=\"17\" height=\"17\" border=\"0\" alt=\"New Match\" />New Match", "NewMatch", new { ruleid = Model[i].id, groupid = Model[i].lstMatches[j].match_group_num }, new AjaxOptions { UpdateTargetId = "Group_rule_" + Model[i].id.ToString() + "_" + Model[i].lstMatches[j].match_group_num, InsertionMode = InsertionMode.InsertBefore, HttpMethod = "Get", LoadingElementId = "msgLoading" })
</div>
}
}
</div>
<!-- end MATCH GROUP -->
<!-- END Loop of Matches -->
<div class="new new_group">
#Ajax.ImageWithTextActionLink("<img src=\"../../Content/Images/new-icon.png\" width=\"17\" height=\"17\" border=\"0\" alt=\"New Group\" />New Group</a>", "NewGroup", new { ruleid = Model[i].id }, new AjaxOptions { UpdateTargetId = "Group_rule_" + Model[i].id.ToString(), InsertionMode = InsertionMode.InsertAfter, HttpMethod = "Get", LoadingElementId = "msgLoading" })
<div class="delete_group">
Delete Group ID:
<input type="text" id='txtdeleteGroupNo_#(Model[i].id)' />
<input type="submit" id='btnDeleteGroup_#(Model[i].id)' value="Delete Group" class="btn btn_delete" style="float: none;" onclick="return DeleteGroup(this);" />
</div>
</div>
<div id="divNewGroup">
</div>
<div class="btn_row">
<input type="reset" name="reset" class="btn" value="Cancel" onclick='DoExpandCollapsh("trMinimised#( Model[i].id)","main_collapsed#( Model[i].id)","main_expanded#( Model[i].id)")'/>
<input type="submit" name="btnSave" class="btn" value="Save" />
<a href="#(Url.Action("DeleteTravelAdsRule", new { id = Model[i].id }))" class="btn btn_delete">
Delete Rule</a>
</div>
</div>
}
</td>
</tr>
}
</tbody>
Here is my Controller
[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult _SaveGroupMatchs(IList<TravelAdsRulesMatchsGroups> mm)
{
try
{
if (mm != null & mm.Count > 0)
{
foreach (var item in mm)
{
List<MatchModel> LMM = item.lstMatches.ToList();
List<MatchModel> LMMUpdate = LMM.Where(t => t.match_id != 0).ToList();
//update
foreach (var mmupdate in LMMUpdate)
{
UpdateMatch(mmupdate);
}
List<MatchModel> LMMSave = LMM.Where(t => t.match_id == 0).ToList();
//save
foreach (var itemSave in LMMSave)
{
itemSave.access = item.access;
itemSave.deployment = item.deployment;
itemSave.match_deployment = item.deployment;
CreateMatch(itemSave);
}
}
}
}
catch { Content("Problem due to Error"); }
return Content("Record Saved Successfully!");
}
I am not getting IList for second and So on rows...
How can I get it, any ideas?
Well I don't really understand what you'd like to do.
If you'd like to save the wole list of TravelAdsRulesMatchsGroups
Use only ONE form which calls your _SaveGroupMatchs action method
If you'd like to save only one TravelAdsRulesMatchsGroups (one submit button for each record),
keep using one form per row and change the action of your controller to
public ActionResult _SaveGroupMatchs(TravelAdsRulesMatchsGroups mm)
then adapt your action method to handle the recording of an instance of TravelAdsRulesMatchsGroups
And, in your View, don't use
#Html.HiddenFor(model => model[i].Id)
If you use this syntax, the rendered Html will be <input ... name="[i].Id" />
(eg. <input ... name="[2].Id" />)
The model binder uses the name to fill your model's properties
Instead, use
#* for your TravelAdsRulesMatchsGroups properties *#
#Html.Hidden("id", Model[i].id)
#* ... and so on ...*#
#* for the lstMatches property, which is a list of MatchModel *#
#for (var j = 0; j < Model[i].lstMatches.Count; j++)
{
#Html.Hidden("lstMatches[" + j.ToString() + "].match_rule_id", Model[i].lstMatches[j].match_rule_id)
#* ... and so on ...*#
}
Note : be carefull, you cannot use nested Html forms !

How i can submit multiple objects inside the form post values to be added

i have a create view to add answers to a question, currently the user can only add one answer at the same time when he clicks on the submit button, instead of this i want the user to be able to insert multiple answers objects into the same view and then the system to add all these new answer objects to the database after the user click on the submit button, my current view looks as the follow:-
#model Elearning.Models.Answer
#{
ViewBag.Title = "Create";
}
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<div id = "remove">
#using (Ajax.BeginForm("Create", "Answer", new AjaxOptions
{
HttpMethod = "Post",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "remove"
}))
{
<div id = "returnedquestion">
#Html.ValidationSummary(true)
<fieldset>
<legend>Answer</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Description)
#Html.ValidationMessageFor(model => model.Description)
</div>
</fieldset>
<input type= "hidden" name = "questionid" value = #ViewBag.questionid>
<input type= "hidden" name = "assessmentid" value = #ViewBag.assessmentid>
<input type="submit" value="Add answer" />
</div>
}
</div>
and the action methods look as the follow:-
public ActionResult Create(int questionid)//, int assessmentid)
{
ViewBag.questionid = questionid;
Answer answer = new Answer();
return PartialView("_answer",answer);
}
//
// POST: /Answer/Create
[HttpPost]
public ActionResult Create(int questionid, Answer a)
{
if (ModelState.IsValid)
{
repository.AddAnswer(a);
repository.Save();
return PartialView("_details",a);
}
return View(a);}
so how i can modify the above code to be able to insert multiple answer objects at the same view and then submit these answers objects at the same time when the user click on the submit button?
Try Post a List
Add input by javascript when user click "Add Answer".
And when submit the form ,it will post all answer data to binding to List
<script>
$(document).ready(function () {
var anwserCount = 1;
$("#addbutton").click(function () {
$("#AnwsersDiv")
.append("<input type='text' name='Anwsers[" + anwserCount + "]'/>");
anwserCount += 1;
});
});
</script>
#using (Html.BeginForm())
{
<div id="AnwsersDiv">
<input type="text" name="Anwsers[0]" />
</div>
<input id="addbutton" type="button" value="Add answer" />
<input type="submit" value="submit" />
}
Model
public class Answer
{
public List<String> Anwsers { get; set; }
}
When submit the form
I think this is what you are looking for
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Conclusion: you should make the post action with ICollection<Answer> Parameter, then it will be easy to get them when you post your main form, and create the appropriate QUESTION object, then save them all with only one submit.

Resources