user9847039
user9847039

Reputation:

Pass ID from URL to Database is going null

My view looks like this My url link http://localhost:63897/UploadImages?id=1361. 1361 is my pr_id. I need to pass the id which is 1361 from url to database, but it is going null.

Here is my controller code:

public ActionResult UploadImages(int id) {
    ViewBag.prid = id;
    return View();
}
[HttpPost]
public ActionResult UploadImages([Bind(Include = "id,photo_url,photo_caption,photo_credit,pr_id")] Photo photos, HttpPostedFileBase photo_file)
{
    if (ModelState.IsValid)
    {
        if (photo_file != null && photo_file.FileName != null && photo_file.FileName != "")
        {
            try
            {
                string path = Path.Combine(Server.MapPath("~/Images/Releases"), Path.GetFileName(photo_file.FileName));
                photo_file.SaveAs(path);
                string f1 = path.Substring(path.LastIndexOf("\\"));
                string[] split = f1.Split('\\');
                string newpath = split[1];
                string imagepath = "~/Images/Releases/" + newpath;
                photos.photo_url = imagepath;
                _db.Photos.Add(photos);
                _db.SaveChanges();

            }
            catch (Exception ex)
            {
                ViewBag.Message = "ERROR:" + ex.Message.ToString();
            }
            return RedirectToAction("List");
        }

    }

    return View();

}

View :

@Html.HiddenFor(model => model.pr_id, new { @Value = ViewBag.id })

Upvotes: 1

Views: 39

Answers (1)

Shyju
Shyju

Reputation: 218722

Your view bag dictionary item's key is prid. But in your view code you are using a different key.

Use ViewBag.prid. Also use the Hidden helper method.

@Html.Hidden("pr_id", new { @value = ViewBag.prid })

Or just write plain HTML and set the value attribute value.

<input type="hidden" name="pr_id" value="@ViewBag.prid" />

Check the view source of the page to confirm the correct value attribute is set to the hidden input element with name pr_id

Assuming you fixed the wrong ViewBag key name, your existing approach will basically generate the below markup

<input Value="23" name="pr_id" type="hidden" value="0" />

Remember, Value != value

This is one major reason i do not use dynamic stuff like ViewBag. You make a silly mistake like this and there are no warnings/errorrs from the IDE/compiler. It just silently fails :( If you use a strongly typed view model, the compiler will complain when you make a silly typo.

Also do not use the *For method and try to manually overwrite the value/id/name etc. The helpers are designed to set the value/name/id attribute values properly. Consider using a view model and use these For methods with them. That will be less code.

If your view model has a property called pr_id, set that property value in your GET action, send that view model to the view and in the view(which is strongly typed to this view model), simply call HiddenFor method on that property

@Html.HiddenFor(a=>a.pr_id);

Upvotes: 2

Related Questions