错误:未为此对象定义无参数构造函数

时间:2019-07-08 20:19:34

标签: c# asp.net-mvc

我正在ASP MVC项目中尝试使用Action- [HttpPost],但是Post Action似乎不起作用。Submit按钮后页面将显示错误。

以下是我的模特:

public class bcSlider
{
    public int sliderID { get; set; }
    public string sliderImage { get; set; }
}

以下是我的ManageOperations:

 public class MngSlider
 {
  public int insertSlider(bcSlider obj)
    {
        int condition = 0;
        try
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ospDB"].ConnectionString);
            SqlCommand cmd = new SqlCommand("usp_InsertConfrence", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@SliderImage", obj.ImageName);
            if (con.State.Equals(ConnectionState.Closed))
                con.Open();
            condition = cmd.ExecuteNonQuery();
            con.Close();
            if (condition > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
  }

以下是控制器:

public class AdminSliderController : Controller
{
    bcSlider modelSlider = new bcSlider();
    MngSlider objSlider = new MngSlider();

  //---------------INSERT---------------//
    // GET: /AdminSlider/AddSlider
    public ActionResult AddSlider()
    {
        bcSlider mSlider = new bcSlider();
        return View(mSlider);
    }


    // POST: /AdminSlider/AddSlider
    [HttpPost]
    public ActionResult AddSlider(HttpPostedFile file, bcSlider mSlider)
    {
            if (ModelState.IsValid)
            {
                //File Available Verification
                if (file != null && file.ContentLength > 0)
                {
                    string fileName = Path.GetFileName(file.FileName);  //FileName
                    string fileExtension = Path.GetExtension(fileName); //FileExtension

                    //Extension Verification
                    if (fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".jpeg" || fileExtension.ToLower() == ".png" || fileExtension.ToLower() == ".pdf" || fileExtension.ToUpper() == ".JPG" || fileExtension.ToUpper() == ".JEPG" || fileExtension.ToUpper() == ".PNG" || fileExtension.ToUpper() == ".PDF")
                    {
                        //FileSize Verification
                        int length = file.ContentLength;
                        if (length <= 1000)
                        {
                            string filepath = Path.Combine(Server.MapPath("~/ImagesDirectory/"), fileName); //FilePath
                            file.SaveAs(filepath);
                            objSlider.insertSlider(mSlider); //Insert to DB
                            ViewBag.Message = "<script>alert('File  Inserted Successfully !')</script>";
                            ModelState.Clear();
                        }
                        else
                        {
                            ViewBag.SizeMessage = "<script>alert('Size Should be of 1MB !')</script>";
                        }
                    }
                    else
                    {
                        ViewBag.ExtensionMessage = "<script>alert('File Not Supported !')</script>";
                    }
                }
                else
                {
                    ViewBag.Message = "<script>alert('Invalid File !')</script>";
                }
            }
            else
            {
                ViewData["result"] = "Registration Not Successful";
            }
                   return RedirectToAction("AddSlider");

    }
   }
 }

以下是htmlcs:

@model OspWebsite.Models.bcSlider

@{
ViewBag.Title = "AddSlider";
Layout = "~/Views/Shared/LayoutAdmin.cshtml";
}

<div class="content-header">

<!-- Page Name -->
<div class="container-fluid">
    <div class="row mb-2">
        <div class="col-sm-6">
            <h1 class="m-0 text-dark">Slider</h1>
        </div><!-- /.col -->
        <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
                <li class="breadcrumb-item"><a href="#">Home</a></li>
                <li class="breadcrumb-item active">Slider</li>
            </ol>
        </div><!-- /.col -->
    </div><!-- /.row -->
</div>
@Html.Raw(ViewBag.Message)
@Html.Raw(ViewBag.SizeMessage)
@Html.Raw(ViewBag.ExtensionMessage)
@using (Html.BeginForm("AddSlider", "AdminSlider", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<!-- Input Forms -->
<div class="row">
    <div class="offset-md-1 mt-5 col-md-10">
        <div class="card card-success">
            <div class="card-header">
                <h3 class="card-title">Add New Slider</h3>
                <div class="card-tools">
                    <button type="button" class="btn btn-tool" data-widget="maximize"><i class="fas fa-expand"></i></button>
                    <button type="button" class="btn btn-tool" data-widget="collapse"><i class="fas fa-minus"></i></button>
                </div>
                <!-- /.card-tools -->
            </div>
            <!-- /.card-header -->
            <div class="card-body">
                <div class="card-body">
                    <div class="form-group">
                        <div class="col-sm-12 ">
                            <label for="inputEmail3"  class="col-sm-4 control-label">Select Slider</label>
                            <div class="custom-file">
                                <label class="custom-file-label" for="exampleInputFile"></label>
                                <input type="file" name="sliderImage" value=@Model.sliderImage class="custom-file-input" id="exampleInputFile">
                            </div>
                        </div>
                    </div>
                </div>
                <!-- /.card-body -->
            </div><div class="card-footer">
                <button type="submit" class="btn btn-success">Upload</button>
                <button type="submit" class="btn btn-default float-right">Cancel</button>
            </div>
            <!-- /.card-footer -->
            <!-- /.card-body -->
        </div>
        <!-- /.card -->
    </div>
    <!-- /.col -->
</div>
   }

 </div>

正在调用GET操作,但是POST操作显示错误。 我正在将图像插入数据库。

1 个答案:

答案 0 :(得分:0)

由于在控制器HttpPostedFile中传递参数而出现错误。

HttpPostedFileBase and HttpPostedFile

肯定不一样。

将方法更改为

[HttpPost]
public ActionResult AddSlider(HttpPostedFileBase sliderImage, bcSlider mSlider)
{
     //your code here
}

通知,我使用了sliderImage,因为那也是您的表格中的内容。

再次,您不知道它是否是拼写错误的表单正在设置文件输入的值

<input type="file" name="sliderImage" value=@Model.sliderImage class="custom-file-input" id="exampleInputFile">

您不应该这样做。

再次,请注意,如果它也不是错字,则您的课程obj.ImageName中没有任何属性,但是{{1}中包含以下内容}方法

insertSlider

有关HttpPostedFileBase和HttpPostedFile的更多信息,请参见 here

仅此而已。