简体   繁体   English

如何创建我在ASP.net MVC中以VARBINARY数据类型存储在数据库中的图像的EDIT方法和EDIT Razor View?

[英]How to create EDIT method and EDIT Razor View for Image which i stored in database in VARBINARY datatype in ASP.net MVC?

Here is my Create Method 这是我的创建方法

  /*  [HttpPost]
    public ActionResult Create(Image newRecipe)
    {
        try
        {
            using (var db = new  MitishaKitchenContext())
            {
                db.Images.Add(newRecipe);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        catch { return View(); }
    }*/

    [HttpPost]
    public ActionResult Create(Image IG)
    {
        // Apply Validation Here



        if (IG == null)
        {
            return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
        }

        if (IG != null && IG.File.ContentLength > (2 * 1024 * 1024))
        {
            ModelState.AddModelError("CustomError", "File size must be less than 2 MB");
            return View();
        }
        if (!(IG.File.ContentType == "image/jpeg" || IG.File.ContentType == "image/gif"))
        {
            ModelState.AddModelError("CustomError", "File type allowed : jpeg and gif");
            return View();
        } 

      //  IG.FileName = IG.File.FileName;
      //  IG.ImageSize = IG.File.ContentLength;

        byte[] data = new byte[IG.File.ContentLength];
        IG.File.InputStream.Read(data, 0, IG.File.ContentLength);

        IG.ImageData = data;
        using (MitishaKitchenContext dc = new MitishaKitchenContext())
        {
            dc.Images.Add(IG);
            dc.SaveChanges();
        }
        return RedirectToAction("Index");
    }

} }

How to create EDIT method and EDIT Razor View for Image which i stored in database in VARBINARY datatype in ASP.net MVC? 如何创建我在ASP.net MVC中以VARBINARY数据类型存储在数据库中的图像的EDIT方法和EDIT Razor View?

Here is my Create View: 这是我的创建视图:

{

 @using (Html.BeginForm("Create","Recipes",null,         FormMethod.Post,new   {enctype="multipart/form-data"}))
        {
          @Html.ValidationSummary(true)


         <fieldset>
    <legend>Image</legend>



    <div class="editor-label">
        @Html.LabelFor(model => model.ImageName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ImageName)
        @Html.ValidationMessageFor(model => model.ImageName)
    </div>
      <div class="editor-label">
        @Html.LabelFor(model => model.Recipe.RecipeName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Recipe.RecipeName)
        @Html.ValidationMessageFor(model => model.Recipe.RecipeName)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Recipe.RecipeDescriptions)
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.Recipe.RecipeDescriptions,new {style = "width:90%;height:400px"})
        @Html.ValidationMessageFor(model => model.Recipe.RecipeDescriptions)

    </div>

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


       <br />
    <table>
                <tr>
                    <td>Select File : </td>
                    <td>
                       @Html.TextBoxFor(Model=> Model.File, new{type="file"})
                        @Html.ValidationMessage("CustomError")
                    </td>
                    <td>
                        <input type="submit" value="Upload" />
                    </td>
                </tr>
            </table> 
enter code here
             </fieldset>
     }  

}

You would need to have the following components in your database table: RecordId, ImageName, RecipeName, RecipeDescriptions, RecipeDescriptions, ImageData. 您将需要在数据库表中包含以下组件:RecordId,ImageName,RecipeName,RecipeDescriptions,RecipeDescriptions,ImageData。

Then in the edit you can do something like this: 然后,在编辑中您可以执行以下操作:

[HttpGet]
public ActionResult Edit(int recordId)
{
        using (var db = new  MitishaKitchenContext())
        {
            // fetch data for the image by Id
            var yourmodel = db.Images.Where(x => x.RecordId == recordId).FirstOrDefault();

            if(yourmodel != null)
            {
                 return View("Create", yourmodel);
            }
        }
        return RedirectToAction("Index");
}

You can use the same view as the Create uses as the model will populate your fields. 您可以使用与“创建”使用的视图相同的视图,因为模型将填充您的字段。 And then you should search if the recordId exists and upsert(insert or update if it already exists). 然后,您应该搜索recordId是否存在并向上插入(如果已经存在,则插入或更新)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ASP.NET MVC 5中通过视图创建/编辑模型 - How to create/edit models via view in asp.net mvc 5 在这种情况下如何创建 Edit 方法? ASP.NET 核心 MVC - How to create an Edit method in this situation? ASP.NET Core MVC 如何不在 ASP.NET MVC“编辑”视图中为每个模型属性重复剃刀代码? - How to do not repeat a razor code in an ASP.NET MVC “edit” view for each model property? 在ASP.NET MVC中编辑视图 - Edit view in asp.net mvc ASP.NEt MVC中的DropDownList编辑方法 - DropDownList in ASP.NEt MVC Edit method 如何在 ASP.NET Core MVC 中的 Edit 方法的下拉字段中显示 selectedValue(来自数据库) - How can I display the selectedValue (from database) in a dropdown-field from the Edit method in ASP.NET Core MVC 如何使用视图模型在asp.net mvc 5中进行编辑? - how to edit in asp.net mvc 5 using view model? 如何在 ASP.NET MVC 的编辑视图中的下拉列表中填充值 - How to fill value in dropdownlist in edit view in ASP.NET MVC 如何在“编辑”视图(ASP.NET MVC)中的Datepicker上获取价值? - How to get value on Datepicker in Edit view (ASP.NET MVC)? 在Asp.net MVC4剃刀视图中编辑和更新动态表中的行 - Edit and update rows in dynamic table in Asp.net mvc4 razor view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM