Reputation: 8711
I have the following model:
public List<string> Results { get; set; }
public List<string> ClubcardNumbers { get; set; }
public bool FindCards {get; set;}
public bool FindDuplicates { get; set; }
public bool AllocatedVouchers { get; set; }
I have the following actions in my controller:
public ActionResult ImportExcel()
{
if (Request.Files["XlFileUpload"].ContentLength > 0)
{
string extension = System.IO.Path.GetExtension(Request.Files["XlFileUpload"].FileName);
string path1 = string.Format("{0}/{1}", Server.MapPath("~/Content/Upload"), Request.Files["XlFileUpload"].FileName);
if (System.IO.File.Exists(path1))
System.IO.File.Delete(path1);
Request.Files["XlFileUpload"].SaveAs(path1);
Session.Add("XlFileUpload", path1);
}
if (Request.Files["DataFileUpload"].ContentLength > 0)
{
string extension = System.IO.Path.GetExtension(Request.Files["DataFileUpload"].FileName);
string path1 = string.Format("{0}/{1}", Server.MapPath("~/Content/Upload"), Request.Files["DataFileUpload"].FileName);
if (System.IO.File.Exists(path1))
System.IO.File.Delete(path1);
Request.Files["DataFileUpload"].SaveAs(path1);
Session.Add("DataFileUpload", path1);
}
MvcUI2.Models.Main model = new Models.Main();
model.ClubcardNumbers = GetCardNumbers();
return View("Main", model);
}
[HttpPost]
public ActionResult RunValidation(Models.Main model)
{
//determine which validations checks to run
if (model.FindCards)
{
FileReader.UnitTests.pronc_vas ut = new FileReader.UnitTests.pronc_vas();
ut.FileToCheck = Session["XlFileUpload"].ToString();
foreach (string cn in model.ClubcardNumbers)
{
ut.ClubcardNumberUnderTest = cn.Split('\t')[0];
ut.ClubcardNumberFound();
//add results to a list to be read by DisplayResults Action
if (ut.Result)
model.Results.Add("PASSED - " + cn + " - " + "Find card number");
else
model.Results.Add("FAILED - " + cn + " - " + "Find card number");
}
}
return View("Main", model);
}
Here is my view:
<div id="body">
<div id="ImportExcel">
<h2>Import</h2>
@using (Html.BeginForm("ImportExcel", "Main", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table>
<tr>
<td>Excel file</td>
<td>
<input type="file" id="XlFileUpload" name="XlFileUpload" /></td>
</tr>
<tr>
<td>Data file</td>
<td>
<input type="file" id="DataFileUpload" name="DataFileUpload" /></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" id="Submit2" name="Submit" value="Submit" /></td>
</tr>
</table>
}
</div>
<div id="DisplayCards">
DisplayCards div
<ul>
@foreach (var item in Model.ClubcardNumbers)
{
<li>
@item
</li>
}
</ul>
</div>
<div id="RunValidation">
@using (Html.BeginForm("RunValidation", "Main"))
{
<table>
<tr>
<td>
@Html.CheckBoxFor(m => m.FindCards)
Find Cards
</td>
</tr>
<tr>
<td>
@Html.CheckBoxFor(m => m.FindDuplicates)
Find Duplicates
</td>
</tr>
<tr>
<td>
@Html.CheckBoxFor(m => m.AllocatedVouchers)
Check Voucher Allocation
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="SubmitValidation" /></td>
</tr>
</table>
}
</div>
<div id="DisplayResults">
<ul>
@foreach (var result in Model.Results)
{
<li>
@result
</li>
}
</ul>
</div>
The user is able to select an Excel file and Data file using the html file input types. The controller then processes these files and populates a list of clubcard numbers into a List within the model. This list is then displayed to the user in the div "DisplayCards". Within the div "RunValidation" the user has the option to select check boxes before posting back to the server. At this point I need the ClubcardNumbers list of the controller to be populated in the model so that it can be processed by the RunValidation Action.
Could anyone please suggest how I can do this?
Upvotes: 1
Views: 4522
Reputation: 1038710
You could include them in your form as hidden fields so that they get passed to the controller when this form is submitted:
@using (Html.BeginForm("RunValidation", "Main"))
{
@for (var i = 0; i < Model.ClubcardNumbers.Count; i++)
{
@Html.HiddenFor(x => x.ClubcardNumbers[i])
}
<table>
...
</table>
}
Upvotes: 5