从类型'System.String'到类型'BH.Models.Resim'的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换

时间:2015-04-02 14:21:36

标签: asp.net-mvc

我有问题。我该如何解决?

此错误消息

  

参数转换类型' System.String'输入' BH.Models.Resim'失败,因为没有类型转换器可以在这些类型之间进行转换。

控制器

 public class HaberYonetimController : Controller
{
    BhContext veritabani = new BhContext();

    // GET: Yonetim/HaberYonetim
    public ActionResult Index()
    {
        return View();
    }

    [ValidateInput(false)]
    public ActionResult HaberEkle()
    {

        ViewBag.Kategori = new SelectList(veritabani.Kategoriler, "Id", "Adi");
        ViewBag.HaberTip = new SelectList(veritabani.HaberTipler, "Id", "Adi");
        ViewBag.Kullanici = new SelectList(veritabani.Kullanicilar, "Id", "KullaniciAd");

        return View();
    }


    public string ResimKaydet(HttpPostedFileBase file)
    {
        Image orj = Image.FromStream(file.InputStream);

        Bitmap kck = new Bitmap(orj, 150, 150);
        Bitmap orta = new Bitmap(orj, 250, 250);
        string dosyaadi = Path.GetFileNameWithoutExtension(file.FileName) + Guid.NewGuid() + Path.GetExtension(file.FileName);

        orj.Save(Server.MapPath("~/Content/images/photos/buyuk/"+dosyaadi));
        orta.Save(Server.MapPath("~/Content/images/photos/orta/" +dosyaadi));
        kck.Save(Server.MapPath("~/Content/images/Photos/kucuk/"+dosyaadi));

        return dosyaadi;


    }








    [HttpPost]
    [ValidateAntiForgeryToken]
    [ValidateInput(false)]
    public ActionResult HaberEkle(Haber haber, HttpPostedFileBase Resim)
    {
        if (ModelState.IsValid)
        {
            haber.Goruntulenme = 0;
            haber.KullaniciID = 3;

            string dosyaadi = ResimKaydet(Resim);

            haber.KucukResimYol = "/Content/images/photos/kucuk/" + dosyaadi;
            haber.ResimYol = "/Content/images/photos/buyuk/" + dosyaadi;
            haber.OrtaResimYol = "/Content/images/photos/orta/" + dosyaadi;

            haber.Aktif = true;
            haber.YayimTarihi = DateTime.Now;


            veritabani.Haberler.Add(haber);
            veritabani.SaveChanges();

            return RedirectToAction("Ekle", "Kategori");

        }
        else
        {
            return View();
        }



    }


    public ActionResult haberx()
    {
        return View();
    }
}

视图

  @model BH.Models.Haber

@{
    ViewBag.Title = "HaberEkle";
    Layout = "~/Areas/Yonetim/Views/Shared/_Admin.cshtml";
}

<br />

<div class="panel panel-default ">
    <div class="panel-heading">
        Haber Ekle
    </div>
    <div class="panel-body">

       @using (Html.BeginForm())
       {
        @Html.AntiForgeryToken()

        <div class="form-horizontal">

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

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

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


                    @Html.TextAreaFor(model => model.Icerik, new { @class = "ckeditor" })

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

            <div class="form-group">
                @Html.Label("Resim", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <input name="Resim" type="file" />

                </div>
            </div>







            <script src="~/Content/Admin/ckeditor2/ckeditor.js"></script>



            <script>
                // Replace the <textarea id="editor3"> with a CKEditor
                // instance, using default configuration.
                CKEDITOR.replace('editor3');
            </script>



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

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





    </div>
</div>








<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

1 个答案:

答案 0 :(得分:0)

确保从控制器操作返回模型对象。

return View(model);

在您的示例中,我怀疑您的问题可能在此处,当模型状态无效时:

[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult HaberEkle(Haber haber, HttpPostedFileBase Resim)
{
    if (ModelState.IsValid)
    {
        haber.Goruntulenme = 0;
        haber.KullaniciID = 3;

        string dosyaadi = ResimKaydet(Resim);

        haber.KucukResimYol = "/Content/images/photos/kucuk/" + dosyaadi;
        haber.ResimYol = "/Content/images/photos/buyuk/" + dosyaadi;
        haber.OrtaResimYol = "/Content/images/photos/orta/" + dosyaadi;

        haber.Aktif = true;
        haber.YayimTarihi = DateTime.Now;


        veritabani.Haberler.Add(haber);
        veritabani.SaveChanges();

        return RedirectToAction("Ekle", "Kategori");

    }
    else
    {
        return View(haber);
    }
}