Wednesday, 18 September 2013

IEnumerable property in model, items duplicated in editor template after a few posts

IEnumerable property in model, items duplicated in editor template after a
few posts

I have an upload plugin, users select a file and click on upload button.
the file is uploaded and the controller action takes the file stored name
and populates it into an editor template.
So once the file is successfully uploaded and the view is returned, the
editor templates shows links to the uploaded file. Now sometimes users can
select multiple files and sometimes they can upload one file per post.
The issue arises when they are uploading single files per post. Lets say i
select a file and click upload, the file gets uploaded fine. I select
another file, the file gets uploaded fine but the editor template in
viewsource is overwritten and I have two duplicate items in the list.
So after the second file upload, if I select another file and click
upload, upon post I see two list items which is correct but they are
duplicate. I am pretty sure that when the view is returned upon the second
post, The editor template has distinct items which is correct but I am not
sure what happens in the HTML.
<div class="bodyContent">
<span class="leftContent">Load/Result/Log Files: </span><span
class="rightContent">
<input name="file" id="file" type="file" />
</span>
</div>
<div class="bodyContent">
<span class="leftContent">Uploaded Files</span><span
class="rightContent">
@if (Model.RunFilesDisplay != null)
{
@Html.EditorFor(x => x.RunFilesDisplay)
}
else
{
@Html.Label("N/A");
}
</span>
</div>
@model RunLog.Domain.Entities.RunLogEntryFilesDisplay
<tr>
<td valign="top">
@* @Html.ActionLink(Model.FileName, "Download", new { @file =
Model.FileName })*@
@Html.DisplayFor(x => x.FileName)
@Html.HiddenFor(x => x.FileName)
@Html.HiddenFor(x => x.FileStoredName)
@Html.HiddenFor(x => x.FileExtension)
@Html.CheckBoxFor(x => x.Checked)
</td>
</tr>
[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(RunLogEntry runLogEntry, String
ServiceRequest,
string submit, string submit1, string
submit2, string submit3,
IEnumerable<HttpPostedFileBase> file,
String AssayPerformanceIssues1,
List<string> Replicates)
{
#region LoadList_JSON_FileUpload
//Save LoadList File:
DateTime uploadDate = DateTime.Now;
string destinationPath =
string.Format("{0}\\{1}\\{2}\\{3}\\",
Server.MapPath("~/App_Data/uploads"),
uploadDate.ToString("yyyy"),
uploadDate.ToString("MMM"),
uploadDate.ToString("dd"));
if (!Directory.Exists(destinationPath))
Directory.CreateDirectory(destinationPath);
string storedFileName =
string.Format("{0}{1}.json",
destinationPath,
System.Guid.NewGuid());
List<string> jsonFileList = new
List<string>();
foreach (var f in file)
{
if (f != null)
{
if
(f.FileName.Contains("json")
||
f.FileName.Contains("JSON"))
{
RunLogEntryFilesDisplay
runFile = new
RunLogEntryFilesDisplay();
string storedFileNames =
string.Format("{0}{1}.json",
destinationPath,
System.Guid.NewGuid());
f.SaveAs(storedFileNames);
jsonFileList.Add(storedFileNames);
runLogEntry.LoadListStoredFileName
=
runLogEntry.LoadListStoredFileName
+ storedFileNames;
runFile.FileStoredName =
storedFileNames;
runFile.FileName =
f.FileName;
runFile.FileExtension =
"JSON";
runFile.Checked = false;
runFiles.Add(runFile);
}
}
}
if (runLogEntry.RunFilesDisplay !=
null &&
runLogEntry.RunFilesDisplay.Count() >
0)
{
foreach (RunLogEntryFilesDisplay
fd in runLogEntry.RunFilesDisplay)
{
runFiles.Add(fd);
jsonFileList.Add(fd.FileStoredName);
}
}
var Pair =
FileImport.CyclesCompleted(jsonFileList);
if (Pair.Key == false)
{
ModelState.AddModelError(string.Empty,
"Unable to parse JSON content.");
;
}
else
{
ModelState.Remove("SPTestsRequested");
ModelState.Remove("LoadListFileName");
ModelState.Remove("LoadListStoredFileName");
ModelState.Remove("RunStatusID");
runLogEntry.SPTestsRequested =
Pair.Value;
runLogEntry.RunFilesDisplay =
runFiles;
}

No comments:

Post a Comment