asp.net MVC - 当我单击提交时,表单不起作用保持在同一页面上



我正在创建一个反馈页面,我希望单击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; }
}

您使用!ModelState.IsValid,您必须使用ModelState.Is ValidTR

        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>
                    }

最新更新