Asp.net MVC编辑方法不起作用 [英] Asp.net MVC edit method not working

查看:94
本文介绍了Asp.net MVC编辑方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是观点:



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);
}









当我编辑任何记录,例如字段TITLE1,它被更新,但其中的图像(Ctl字段)被删除。我不知道它为什么会发生在这里。我已经调试了代码,在编辑方法中有





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

,文件为空。



我尝试了什么:



i刚解释了上面的问题和我的尝试。

in the edit method , file comes null.

What I have tried:

i just explained the problem above and my try.

推荐答案





当您编辑表单而不选择任何文件时,
Hi,

When you edit the form and not select any file then
HttpPostedFileBase file

将为null。这就是为什么Ctl列将成为银行。



在这种情况下你应该使用

will be null.that's why Ctl column will be bank.

In this case you should use

contentHtml.Ctl=contentHtml.Ctl





表示当用户没有选择文件时,您应该在Ctl列中设置旧文件名。



希望这项工作..



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

Hope this work..


这篇关于Asp.net MVC编辑方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆