Hi There
I am building a very simple asp.net MVC file upload form. Currently I have a problem creating the new database object that stores info on said file.
The code for the action method looks as follows:
[Authorize(Roles = "Admin")]
public ActionResult AddFile(Guid? id)
{
var org = organisationRepository.GetOrganisationByID(id.Value);
Quote newFile = new Quote();
newFile.Organisation = org;
newFile.QuotedBy = User.Identity.Name;
return View("AddFile", newFile);
}
The problem is that the value of newFile.Organisation is lost when the form is posted back. I suppose EF does not provide a value for OrganisationID at this stage.
[Authorize(Roles = "Admin")]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddFile(Quote quote)
{
//save the file to the FS - nice!
if (ModelState.IsValid)
{
try
{
foreach (string inputTagName in Request.Files)
{
HttpPostedFileBase file = Request.Files[inputTagName];
if (file.ContentLength > 0)
{
string filePath = Path.Combine(HttpContext.Server.MapPath("~/Content/TempOrgFiles/")
, Path.GetFileName(file.FileName));
file.SaveAs(filePath);
quote.QuoteURL = file.FileName;
}
}
surveyRepository.AddQuote(quote);
surveyRepository.Save();
}
catch (Exception ex)
{
//add model state errors
ViewData["error"] = ex.Message;
}
}
return View("AddFile");
}
If this were linq to sql I would simplt set the OrganisationID, but with it being EF that is not possible (at least with my setup)
Any ideas as the best way to handle these situations? (save for doin something crazy like setting a hidden form field to the organisaionid and setting it in the post method)