1
votes

My view

@using (Html.BeginForm("Edit", "Referance", FormMethod.Post, new { enctype = form-data" })) { @Html.ValidationSummary(true)

<fieldset>
    <legend>Referance</legend>

    @Html.HiddenFor(model => model.referanceId)
    <div class="editor-label">
        Dil
    </div>
    <div class="editor-field">
        @Html.DropDownList("languageId", String.Empty)
        @Html.ValidationMessageFor(model => model.languageId)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.name)
        @Html.ValidationMessageFor(model => model.name)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.description)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.description)
        @Html.ValidationMessageFor(model => model.description)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.path)
    </div>
    <div class="editor-field">
        <input type="file" id="fuPhoto" name="fuPhoto" value="123" /> 
        @Html.ValidationMessageFor(model => model.path)
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

My Controller

public ActionResult Edit(int id = 0)
        {
            Referance oReferance = db.Referance.Find(id);
            ViewBag.languageId = new SelectList(db.Language, "languageId", "name");
            if (oReferance == null)
            {
                return HttpNotFound();
            }
            return View(oReferance);
        }
[HttpPost]
        public ActionResult Edit(HttpPostedFileBase fuPhoto, Referance oReferance)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (oReferance.path != null && fuPhoto == null)
                        oReferance.path = myHelper.saveFile(fuPhoto, "Uploads");

                    db.Entry(oReferance).State = EntityState.Modified;
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

I try update to my db but when I dont select a file with fileUpload, it sets null to db. I controlled it in post edit for it does not set null value, but it still sets. How can I fix it or how can set the path in my db to fileupload when load my edit form.

thanks for helps.

2

2 Answers

1
votes

Just include a hidden field for the file property.

<div class="editor-field">
    <input type="file" id="fuPhoto" name="fuPhoto" value="123" /> 
    @Html.HiddenFor(model => model.path)
    @Html.ValidationMessageFor(model => model.path)
</div>

That way the value will be posted along with the rest of the form and then you update it with the posted file in your controller action.

Updated Controller :

public ActionResult Edit(HttpPostedFileBase fuPhoto, Referance oReferance)
{
    try
    {
        if (ModelState.IsValid)
        {
            // at this point oReferance.path contains the value that was posted with the hidden field
            // if a file was selected, upload and update the model's path property
            if (fuPhoto != null)
                oReferance.path = myHelper.saveFile(fuPhoto, "Uploads");

            // save model   
            db.Entry(oReferance).State = EntityState.Modified;
            db.SaveChanges();

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }
}

As you can see I removed the oReferance.path != null of your if statement since you only need to test whether a new file was uploaded or not, the old value is irrelevant and will be kept if there was no file selected.

1
votes

When you do this directly from the model binded object, you are telling EF to override all the properties in the oReferance object.

if ( fuPhoto == null)
     oReferance.path = myHelper.saveFile(fuPhoto, "Uploads");

     db.Entry(oReferance).State = EntityState.Modified;
     db.SaveChanges();

Since you are not binding directlry from oRefreance.path into the view, the path is no longer avaliable in the post method.

You can add a hidden in the view @if (!string.IsNullorEmpty(Model.path)) { Model.path }

<input type="file" id="fuPhoto" name="fuPhoto" value="123" /> 

@Html.HiddenFor(model=> model.path)
@Html.ValidationMessageFor(model => model.path)

This will ensure the path will be binded back to the OReference object.