简体   繁体   中英

Problem with related entity on asp.net MVC create form using entity framework

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)

You can store OrganisationID in session

[Authorize(Roles = "Admin")]
public ActionResult AddFile(Guid? id)
{
Session["OrganisationID"] = id;
}

and then set EntityKey with something like:

quote.OrganisationReference.EntityKey = new EntityKey("ContainerName.OrganizationSet","ID",Session["OrganisationID"])

If You have organisation ID in URL, You can change post function to:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddFile(Quote quote,Guid? id)

and retrieve it automatically (with adequate routing).

You don't neet to get organisation from repository during get, just store ID.

The data is lost because it is stateless . Your quote object in the GET is not the same quote object as passed in the POST. It's only partially hydrated due to the native binding in MVC (by property names). You'll either need to setup a CustomModelBinder or requery for your Quote object in the controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM