应用程序中的服务器错误 - 尝试将图像添加到数据库时无法找到资源

时间:2015-07-09 15:14:21

标签: asp.net asp.net-mvc-4 entity-framework-6

添加此行后我收到此错误。

@using (Html.BeginForm(
    "CourseRegister", 
    "CoursePlanController", 
    FormMethod.Post, 
    new { enctype = "multipart/form-data" })) 

这是我的控制器(CoursePlanController)并创建动作。

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult CourseRegister(CourseCategory model, HttpPostedFileBase upload)
{

    try
    {
        if (ModelState.IsValid)
        {

            if (upload != null && upload.ContentLength > 0)
            {
                var avatar = new File
                {
                    FileName = System.IO.Path.GetFileName(upload.FileName),
                    FileType = FileType.Avatar,
                    ContentType = upload.ContentType
                };
                using (var reader = new System.IO.BinaryReader(upload.InputStream))
                {
                    avatar.Content = reader.ReadBytes(upload.ContentLength);
                }
                model.Files = new List<File> { avatar };
            }

            try
            {
                CourseCategory ca = new CourseCategory() { courseField = model.courseField, courseSubject = model.courseSubject, courseName = model.courseName, StartDate = model.StartDate, price = model.price, courseLevel = model.courseLevel, duration = model.duration, qualifications = model.qualifications, overview = model.overview, description = model.description, institute = model.institute, contact = model.contact, email = model.email,Files = model.Files };
                this._courseManagerSvcInstance.CreateNewCourse(ca);

                ViewBag.SuccessMessage = "<p>Success!</p>";
                return RedirectToAction("Index");
            }
            catch (MembershipCreateUserException e)
            {
                //ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }
    }
    catch (RetryLimitExceededException /* dex */)
    {
        ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
    }


    return View();
}

这是我的观点。 (CourseRegister.cshtml)

   @model SmartJob.Domain.Models.CourseCategory

   @{
             ViewBag.Title = "CourseRegister";
   }



   @using (Html.BeginForm("CourseRegister",     "CourseCategory", FormMethod.Post, new { enctype = "multipart/form-data" }))


    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <fieldset>
            @*<legend>Course Category</legend>*@

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


                @Html.DropDownListFor(model => model.courseField, new SelectList(SmartJob.Web.UI.Helper.ProfessionalCourseDropDownHelper.CourseCategoriesDropDown, "Value", "Value", 0), "", new { @class = "TextBoxFor" })
                @Html.ValidationMessageFor(model => model.courseField, null, new { @class = "errors" })

            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.courseSubject)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.courseSubject)
                @Html.ValidationMessageFor(model => model.courseSubject, null, new { @class = "errors" })
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.courseName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.courseName)
                @Html.ValidationMessageFor(model => model.courseName, null, new { @class = "errors" })
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.StartDate)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.StartDate)
                @Html.ValidationMessageFor(model => model.StartDate, null, new { @class = "errors" })
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.price)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.price)
                @Html.ValidationMessageFor(model => model.price, null, new { @class = "errors" })
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.courseLevel)
            </div>
            <div class="editor-field">
                @*@Html.EditorFor(model => model.courseType)
                @Html.ValidationMessageFor(model => model.courseType, null, new { @class = "errors" })*@

                @Html.DropDownListFor(model => model.courseLevel, new SelectList(SmartJob.Web.UI.Helper.ProfessionalCourseDropDownHelper.CourseLevelsDropDrown, "Value", "Value", 0), "", new { @class = "TextBoxFor" })
                @Html.ValidationMessageFor(model => model.courseLevel, null, new { @class = "errors" })

             </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.duration)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.duration)
                @Html.ValidationMessageFor(model => model.duration, null, new { @class = "errors" })
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.qualifications)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.qualifications)
                @Html.ValidationMessageFor(model => model.qualifications, null, new { @class = "errors" })
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.overview)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.overview)
                @Html.ValidationMessageFor(model => model.overview, null, new { @class = "errors" })
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.description)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.description)
                @Html.ValidationMessageFor(model => model.description, null, new { @class = "errors" })
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.institute)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.institute)
                @Html.ValidationMessageFor(model => model.institute, null, new { @class = "errors" })
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.contact)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.contact)
                @Html.ValidationMessageFor(model => model.contact, null, new { @class = "errors" })
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.email)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.email)
                @Html.ValidationMessageFor(model => model.email, null, new { @class = "errors" })
            </div>

            @*------------upload Image------------------*@
            <div class="editor-label">
                @Html.LabelFor(model => model.Files)
            </div>
            <div class="editor-field">
                <input type="file" id="Avatar" name="upload" />
            </div>

            @*------------------------------------------*@

            <p>
            <p></p>
            <input class="btn btn-default" type="submit" value="Create" />
            </p>
        </fieldset>
    }

这是我的模型类(CourseCategory.cs)

public class CourseCategory
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [Required(ErrorMessage = "Please Select Field of Study")]
    [Display(Name = "Field of Study")]
    public string courseField { get; set; }

    [Required(ErrorMessage = "Please Select Course Subject")]
    [Display(Name = "Course Subject")]
    public string courseSubject { get; set; }


    [Required(ErrorMessage = "Please Select Course Name")]
    [Display(Name = "Course Name", Prompt = "Course Name")]
    public string courseName { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    [Display(Name = "Start Date")]
    public DateTime StartDate { get; set; }

    [Display(Name = "Price (Rs.)")]
    public double price { get; set; }

    [Required(ErrorMessage = "Please Select Course Type")]
    [Display(Name = "Course/Program Level")]
    public string courseLevel { get; set; }

    [Display(Name = "Duration")]
    public string duration { get; set; }

    [Display(Name = "Qualifications")]
    public string qualifications { get; set; }

    [Display(Name = "Overview")]
    [DataType(DataType.MultilineText)]
    public string overview { get; set; }

    [Display(Name = "Description")]
    [DataType(DataType.MultilineText)]
    public string description { get; set; }

    [Display(Name = "Institute")]
    public string institute { get; set; }

    [Display(Name = "Contact Details" )]
    [RegularExpression(@"^\d{10}$", ErrorMessage="Please provide valid contact number (ex : 0123456789) ")]
    public string contact { get; set; }

    [Display(Name = "Contact E-Mail")]
    [RegularExpression(@"^([0-9a-zA-Z]([\+\-_\.][0-9a-zA-Z]+)*)+@(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z0-9]{2,3})$",
                ErrorMessage = "Please provide valid email address")]
    public string email { get; set; }

    [Display(Name = "Attachments")]
    public virtual ICollection<File> Files { get; set; }

}

1 个答案:

答案 0 :(得分:0)

您实际上是在收到404错误,因为您为BeginForm方法提供了错误的控制器名称。在MVC的土地上,MyController等控制器类通常用于删除了Controller后缀的URL。所以你需要改变这一行:

@using (Html.BeginForm(
    "CourseRegister", 
    "CoursePlan", 
    FormMethod.Post, 
    new { enctype = "multipart/form-data" }))