单击“提交”时,表单不起作用

时间:2016-08-04 08:22:16

标签: asp.net-mvc razor c#-3.0

我正在创建反馈页面,我希望此页面在我点击submit按钮后转到发送页面。我一直在尝试使用此代码,当我点击submit时,它只会停留在同一页面上...

这是我的观点:

@using (Html.BeginForm("Feedback", "Home", FormMethod.Post))
{
    @Html.ValidationSummary()
    @Html.AntiForgeryToken()

    @Html.ValidationSummary("", new {@class = "text-danger"})
    <div class="form-group">
        @Html.LabelFor(m => m.Name, new {@class = "col-md-2 control-label"})
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.Name, new {@class = "form-control", placeholder = "Your Name"})
        </div>
    </div>

    @Html.ValidationSummary("", new {@class = "text-danger"})
    <div class="form-group">
        @Html.LabelFor(model => model.Email, "Email", new {@class = "control-label col-sm-2"})
        <div class="col-md-10">
            @Html.EditorFor(m => m.Email, new {htmlAttributes = new {@class = "form-control", placeholder = "Email Address"}})
        </div>
    </div>

    @Html.ValidationSummary("", new {@class = "text-danger"})
    <div class="form-group">
        @Html.LabelFor(m => m.Cell, new {@class = "col-md-2 control-label"})
        <div class="col-md-10">
            @Html.EditorFor(m => m.Cell, new {htmlAttributes = new {@class = "form-control", placeholder = "Phone Number", type = "text"}})
        </div>
    </div>

    @Html.ValidationSummary("", new {@class = "text-danger"})
    <div class="form-group">
        @Html.LabelFor(m => m.Message, new {@class = "col-md-2 control-label"})
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.Message, new {@class = "form-control", placeholder = "Comments", rows = "4"})
        </div>
    </div>

    <div class="col-sm-6 col-sm-offset-3">
        <div class="btn-toolbar">
            <button class="btn-raised btn-primary btn" id="submit">Submit
                <div class="ripple-container"></div>
            </button>
            <button class="btn btn-default">Cancel</button>
        </div>
    </div>
}

我的控制器:

[HttpGet]
public ActionResult Feedback()
{
     ViewBag.Message = "Your contact page.";
     return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Feedback(FeedbackViewModel model)
{
    if (!ModelState.IsValid)
    {
        var item = new FeedbackViewModel()
                {
                    Name = model.Name,
                    Email = model.Email,
                    Cell = model.Cell,
                    Message = model.Message,
                };

        // TODO: Add success message to ViewBag / Data so notification will be displayed
        return RedirectToAction("Sent");
    }

    // TODO Send email in c#
    return View(model);
}

我的模特:

public class FeedbackViewModel
{
        [Required]
        public string Name { get; set; }

        [Required]
        [EmailAddress]
        public String Email { get; set; }

        [MinLength(10)]
        [StringLength(13, ErrorMessage = "Please enter a valid phone number")]
        public string Cell { get; set; }

        [Required]
        [StringLength(200, ErrorMessage = "Please enter more than 20 characters and less than 200", MinimumLength = 20)]
        public string Message { get; set; }
}

1 个答案:

答案 0 :(得分:1)

你使用!ModelState.IsValid ,你必须使用 ModelState.IsValid

        if (ModelState.IsValid)
        {
            var item = new FeedbackViewModel()
            {
                Name = model.Name,
                Email = model.Email,
                Cell = model.Cell,
                Message = model.Message,
            };




            //TODO: Add success message to ViewBag / Data so notification will be displayed
            return RedirectToAction("Sent");

        }


        //TODOL Send email in c#
        return View(model);

然后添加__ @ Html.ValidationMessageFor(..)__来查看

@using (Html.BeginForm("Feedback", "Home", FormMethod.Post))
                    {
                        @Html.ValidationSummary()
                        @Html.AntiForgeryToken()

                    @Html.ValidationSummary("", new {@class = "text-danger"})
                    <div class="form-group">
                        @Html.LabelFor(m => m.Name, new {@class = "col-md-2 control-label"})
                        <div class="col-md-10">
                        @Html.TextBoxFor(m => m.Name, new {@class = "form-control", placeholder = "Your Name"})
                        @Html.ValidationMessageFor(m => m.Name)
                        </div>
                    </div>

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

                    @Html.ValidationSummary("", new {@class = "text-danger"})
                    <div class="form-group">
                        @Html.LabelFor(m => m.Cell, new {@class = "col-md-2 control-label"})
                        <div class="col-md-10">
                            @Html.EditorFor(m => m.Cell, new {htmlAttributes = new {@class = "form-control", placeholder = "Phone Number", type = "text"}})
                            @Html.ValidationMessageFor(m => m.Cell)
                        </div>
                    </div>

                    @Html.ValidationSummary("", new {@class = "text-danger"})
                    <div class="form-group">
                        @Html.LabelFor(m => m.Message, new {@class = "col-md-2 control-label"})
                        <div class="col-md-10">
                            @Html.TextBoxFor(m => m.Message, new {@class = "form-control", placeholder = "Comments", rows = "4"})
                            @Html.ValidationMessageFor(m => m.Message)
                        </div>
                    </div>
                    <div class="col-sm-6 col-sm-offset-3">
                        <div class="btn-toolbar">
                           <input type="submit" value="Submit" class="btn-raised btn-primary btn" />
                           <button class="btn btn-default">Cancel</button>
                            </div>
                        </div>
                    }