Html.BeginForm FormMethod.Post不起作用

时间:2017-11-08 19:37:15

标签: c# asp.net-mvc razor

我想将数据发布到HttpPost的操作中。我总是这样做,但这次我不知道这里发生了什么。提交按钮发送视图数据到获取操作(我的意思是详细操作是HttpGet)按钮类型是如您所见提交的,并且在Html.BeginForm中,如您所见,我使用了FormMethod.Post但只是发送了dar。我在这里搜索但一无所获。

查看:



@using MF_Urmia.Models.ViewModels
@using Kendo.Mvc.UI
@model CourseVm
@{
    Layout = "~/Areas/Admin/Views/Shared/_LayoutPage.cshtml";
}
@using(Html.BeginForm("Detail", "Course", FormMethod.Post))
{
    @Html.HiddenFor(m => m.CourseId)
    @Html.ValidationSummary(true)
    <div class="form-column">
        <h1 class="form-title-box">
	        <span class="form-title">
                @ViewBag.title
            </span>
        </h1>

        <div class="frm-group">
            <div class="form-label form-required">
                @Html.LabelFor(m => m.CourseName)
            </div>
            <div class="form-field">
                @Html.TextBoxFor(m => m.CourseName, new { @class = "k-textbox" })
            </div>
        </div>
        <div class="frm-group">
            <div class="form-label form-required">
                @Html.LabelFor(m => m.TypeName)
            </div>
            <div class="form-field">
                @(Html.Kendo().DropDownListFor(m => m.CourseTypeId)
                    .BindTo((IEnumerable<SelectListItem>)ViewBag.CourseTypeList)
                    .OptionLabel("(Select One)")
                    .DataValueField("Value")
                    .DataTextField("Text")
                    .HtmlAttributes(new { id = "CourseType" }))
            </div>
        </div>
        <div class="frm-group">
            <div class="form-label form-required">
                @Html.LabelFor(m => m.Price)
            </div>
            <div class="form-field">
                @Html.TextBoxFor(m => m.Price, new { @class = "k-textbox", @type = "number"})
            </div>
        </div>
        <br />
        <div class="frm-group">
            <div class="form-label form-required">
                @Html.LabelFor(m => m.Copacity)
            </div>
            <div class="form-field">
                @Html.TextBoxFor(m => m.Copacity, new { @class = "k-textbox", @type = "number" })
            </div>
        </div>
        <div class="frm-group">
            <div class="form-label form-required">
                @Html.LabelFor(m => m.TeacherName)
            </div>
            <div class="form-field">
                @Html.TextBoxFor(m => m.TeacherName, new { @class = "k-textbox" })
            </div>
        </div>
        <div class="frm-group">
            <div class="form-label form-required">
                @Html.LabelFor(m => m.DurationTime)
            </div>
            <div class="form-field">
                @Html.TextBoxFor(m => m.DurationTime, new { @class = "k-textbox", @type = "number" })
            </div>
        </div>
        <br />
        <div class="frm-group">
            <div class="form-label form-required">
                @Html.LabelFor(m => m.ClassStart)
            </div>
            <div class="form-field">
                @Html.TextBoxFor(model => model.ClassStart, new { @class = "form-control kendo-date-picker", value = "", placeholder = "مثال :1396/05/20" })
            </div>
        </div>
        <div class="frm-group">
            <div class="form-label">
                @Html.LabelFor(m => m.RegisterLimit)
            </div>
            <div class="form-field">
                @Html.TextBoxFor(model => model.RegisterLimit, new { @class = "form-control kendo-date-picker", value = "", placeholder = "مثال :1396/05/20" })
            </div>
        </div>
        <hr />
        <div class="frm-group">
            <div class="form-label form-required">
                @Html.LabelFor(m => m.CourseImage)
            </div>
            <div class="form-field-2xl">
                @Html.Kendo().Upload().Name("ImageFile").Messages(m => m.Select("select")).Multiple(false)
            </div>
        </div>
        <hr />
        <div class="frm-group">
            <div class="form-label">
                @Html.LabelFor(m => m.Description)
            </div>
            <div class="form-field full-width">
                @Html.TextAreaFor(m => m.Description, new { @class = "k-textbox ck-editor-box" })
            </div>
        </div>
        <hr />
        <input value="submit" type="submit" class="btn btn-success form-btn" />
    </div>
}
&#13;
&#13;
&#13;

控制器:

public ActionResult Detail(int? id)
{
    var courseId = id;
    using (var db = new MFUEntities())
    {
        var courseTypeList = db.CourseType.ToList();
        ViewBag.CourseTypeList = new SelectList(courseTypeList,
            nameof(CourseType.CourseTypeId), nameof(CourseType.DisplayName));

        if (courseId.HasValue)
        {
            //ويرايش
            ViewBag.title = "Edit";

            return View();
        }
        else
        {
            //ثبت
            ViewBag.title = "Create";

            return View();
        }
    }
}

[HttpPost]
public ActionResult Detail(CourseVm vm)
{
    var courseId = vm.CourseId;
    using (var db = new MFUEntities())
    {
        if (courseId > 0)
        {
            //Edit
            var course = db.Course.FirstOrDefault(c => c.CourseId == vm.CourseId);
            if (course == null)
            {
                return HttpNotFound();
            }

            //ويرايش عکس دوره
            var oldImage = course.CourseImage;
            //عکس قبلي را پاک کند
            System.IO.File.Delete(Server.MapPath("/Images/CourseImage/Images/" + oldImage));
            System.IO.File.Delete(Server.MapPath("/Images/CourseImage/Thumb/" + oldImage));

            vm.CourseImage = Guid.NewGuid().ToString().Replace("-", "") +
                         Path.GetExtension(vm.ImageFile.FileName);

            vm.ImageFile.SaveAs(Server.MapPath("/Images/CourseImage/Images/" + vm.CourseImage));

            ImageResizer img = new ImageResizer();
            img.Resize(Server.MapPath("/Images/CourseImage/Images/" + vm.CourseImage),
                Server.MapPath("/Images/CourseImage/Thumb/" + vm.CourseImage));

            Mapper.Map(vm, course);
        }
        else
        {
            //Create
            //ثبت عکس دوره
            vm.CourseImage = Guid.NewGuid().ToString().Replace("-", "") +
                         Path.GetExtension(vm.ImageFile.FileName);

            vm.ImageFile.SaveAs(Server.MapPath("/Images/CourseImage/Images/" + vm.CourseImage));

            ImageResizer img = new ImageResizer();
            img.Resize(Server.MapPath("/Images/CourseImage/Images/" + vm.CourseImage),
                Server.MapPath("/Images/CourseImage/Thumb/" + vm.CourseImage));

            var course = new Course();
            Mapper.Map(vm, course);

            course.Remained = vm.Copacity;
            course.CreateDate = DateTime.Now;
            db.Course.Add(course);
        }

        db.SaveChanges();
        return RedirectToAction("Index", "Course");
    }
}

0 个答案:

没有答案