如何使用图像保存模型MVC 4 [英] How to save Model with Image in MVC 4

查看:134
本文介绍了如何使用图像保存模型MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I codeD这就节省了产品完美的数据库...产品有标识,code,NAME属性产品创造页面。现在我需要添加映像。

I coded a "Product Create" page which saves a product to the database perfectly... Product has ID,CODE,NAME properties. Now I need to add Image as well.

我的老code低于

[HttpPost]
public ActionResult ProductsCreate(ProductDetailViewModel prod_)
{
    if (ModelState.IsValid)
    {
       db.AY_PRODUCTS.Add(prod_.product);
       db.SaveChanges();
    }      
}

我应该如何改变视图和控制器来保存图像模型?

How should I change the View and Controller to save the Model with Image?

推荐答案

添加属性HttpPostedFileBase到模型。用它在你看来张贴的形象。

Add property HttpPostedFileBase to your model. Use it in your view to post image.

public HttpPostedFileBase YourFile { get; set; }

在你的行动,这种图像保存到你的位置。

In your action, save this image to your location.

在您查看:记得 ENCTYPE =的multipart / form-data的表单中的

In your View: remember enctype = "multipart/form-data" in your form.

<input name="YourFile" id="YourFile" type="file" accept="image/jpeg, image/png,image/jpg" value="">

提示:也许这是没有必要的问题。你应该命名的动作只是创建。如果我没有错,你有ProductController的,对不对?所以,你只需要命名此创建,没必要ProductsCreate。

Tips: Maybe this is no need for your problem. You should name the action just "Create". If i'm not wrong, you have ProductController, right? So you just need name this to Create, no need "ProductsCreate".

这篇关于如何使用图像保存模型MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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