Click here to Skip to main content
15,881,042 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is the view :

@model CMSFC.Models.ContentHtml

@{
    ViewBag.Title = "Edit";
}


<script src="~/Themes/ckeditor/ckeditor.js"></script> 
<h2>Edit</h2>


@using (Html.BeginForm("Edit", "ContentHtml", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>ContentHtml</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Id)

        <div class="form-group">
            @Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Content, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })
            </div>
        </div>

     

        <div class="form-group">
            @Html.LabelFor(model => model.Ctl, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">

                <img src="@Model.Ctl" alt="Image" />

                <input type="file" name="file" value="Browse" />

            </div>
        </div>



        <div class="form-group">
            @Html.LabelFor(model => model.PublishDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.PublishDate, "{0:dd MMM yyyy}", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.PublishDate, "", new { @class = "text-danger" })
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(model => model.ExpiryDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.ExpiryDate, "{0:dd MMM yyyy}", new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.ExpiryDate, "", new { @class = "text-danger" })
            </div>
        </div>





        <div class="form-group">
            @Html.LabelFor(model => model.Exclude, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Exclude, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Exclude, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Title1, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title1, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Title1, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Title2, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title2, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Title2, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Title3, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title3, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Title3, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Title4, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title4, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Title4, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Title5, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title5, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Title5, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ShortDescription1, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.ShortDescription1, new { cols = 60, rows = 6, @class = "form-control" })
                <script type="text/javascript"> CKEDITOR.replace('ShortDescription1'); </script>
                @Html.ValidationMessageFor(model => model.ShortDescription1, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ShortDescription2, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.ShortDescription2, new { cols = 60, rows = 6, @class = "form-control" })
                <script type="text/javascript"> CKEDITOR.replace('ShortDescription2'); </script>
                @Html.ValidationMessageFor(model => model.ShortDescription2, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ShortDescription3, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.ShortDescription3, new { cols = 60, rows = 6, @class = "form-control" })
                <script type="text/javascript"> CKEDITOR.replace('ShortDescription3'); </script>
                @Html.ValidationMessageFor(model => model.ShortDescription3, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ShortDescription4, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.ShortDescription4, new { cols = 60, rows = 6, @class = "form-control" })
                <script type="text/javascript"> CKEDITOR.replace('ShortDescription4'); </script>
                @Html.ValidationMessageFor(model => model.ShortDescription4, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ShortDescription5, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.ShortDescription5, new { cols = 60, rows = 6, @class = "form-control" })
                <script type="text/javascript"> CKEDITOR.replace('ShortDescription5'); </script>
                @Html.ValidationMessageFor(model => model.ShortDescription5, "", new { @class = "text-danger" })
            </div>
        </div>

       



        <div class="form-group">
            @Html.LabelFor(model => model.LongDescription1, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.LongDescription1, new { cols = 60, rows = 6, @class = "form-control" })
                <script type="text/javascript"> CKEDITOR.replace('LongDescription1'); </script>

                @Html.ValidationMessageFor(model => model.LongDescription1, "", new { @class = "text-danger" })
            </div>
        </div>






        <div class="form-group">
            @Html.LabelFor(model => model.LongDescription2, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.LongDescription2, new { cols = 60, rows = 6, @class = "form-control" })
                <script type="text/javascript"> CKEDITOR.replace('LongDescription2'); </script>
                @Html.ValidationMessageFor(model => model.LongDescription2, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LongDescription3, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.LongDescription3, new { cols = 60, rows = 6, @class = "form-control" })
                <script type="text/javascript"> CKEDITOR.replace('LongDescription3'); </script>
                @Html.ValidationMessageFor(model => model.LongDescription3, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LongDescription4, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.LongDescription4, new { cols = 60, rows = 6, @class = "form-control" })
                <script type="text/javascript"> CKEDITOR.replace('LongDescription4'); </script>
                @Html.ValidationMessageFor(model => model.LongDescription4, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LongDescription5, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.LongDescription5, new { cols = 60, rows = 6, @class = "form-control" })
                <script type="text/javascript"> CKEDITOR.replace('LongDescription5'); </script>
                @Html.ValidationMessageFor(model => model.LongDescription5, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.InsertDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.InsertDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.InsertDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.InsertBy, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.InsertBy, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.InsertBy, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.OrderBy, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.OrderBy, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.OrderBy, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}



And this is the method :

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,Content,Ctl,PublishDate,ExpiryDate,Exclude,Title1,Title2,Title3,Title4,Title5,ShortDescription1,ShortDescription2,ShortDescription3,ShortDescription4,ShortDescription5,LongDescription1,LongDescription2,LongDescription3,LongDescription4,LongDescription5,InsertDate,InsertBy,OrderBy")] ContentHtml contentHtml, HttpPostedFileBase file)
{
    return CreateEdit(file, contentHtml, false);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Content,Ctl,PublishDate,ExpiryDate,Exclude,Title1,Title2,Title3,Title4,Title5,ShortDescription1,ShortDescription2,ShortDescription3,ShortDescription4,ShortDescription5,LongDescription1,LongDescription2,LongDescription3,LongDescription4,LongDescription5,InsertDate,InsertBy,OrderBy")] ContentHtml contentHtml, HttpPostedFileBase file)
{
    return CreateEdit(file, contentHtml, true);
}

private ActionResult CreateEdit(HttpPostedFileBase file, ContentHtml contentHtml, bool isCreate)
{
    if (ModelState.IsValid)
    {
        if (file != null)
        {
            file.SaveAs(HttpContext.Server.MapPath("/Content/Images/") + file.FileName);
            contentHtml.Ctl = ("/Content/Images/") + file.FileName;
        }

        if (isCreate)
        {


            db.ContentHtmls.Add(contentHtml);
            db.SaveChanges();

        }
        else
        {
            db.Entry(contentHtml).State = EntityState.Modified;
            db.SaveChanges();
        }
        return RedirectToAction("Index");
    }



    return View(contentHtml);
}




When i edit any record , for example field TITLE1 , it is updated , but the Image (Ctl field)in it is deleted . I dont know why its happening here.I have debug the code , with here
HttpPostedFileBase file
in the edit method , file comes null.

What I have tried:

i just explained the problem above and my try.
Posted
Updated 24-Aug-17 4:25am

1 solution

Hi,

When you edit the form and not select any file then
HttpPostedFileBase file
will be null.that's why Ctl column will be bank.

In this case you should use
contentHtml.Ctl=contentHtml.Ctl


means when user not select file then you should set the old file name in Ctl column.

Hope this work..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900