如何使用ASP.net MVC将对象传递给特定视图

时间:2016-12-30 11:03:33

标签: asp.net-mvc

我有一个页面,它有一个表单,并且每个人都能正常工作。 我的问题是当我的模型状态无效时,如何返回联系我们索引视图而不创建视图

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id,Name,Email,Phone,Message,Date")] Contact_US contact_US)
    {
        if (ModelState.IsValid)
        {
            db.Contact_US.Add(contact_US);
            db.SaveChanges();

            MailMessage mail = new MailMessage();
            mail.To.Add("");
            mail.From = new MailAddress("");
            mail.Subject = contact_US.Email;
            string body = contact_US.Message;
            mail.Body = "Phone:" + contact_US.Phone + "<br />Name:" + contact_US.Name +"<br />Email:"+contact_US.Email+ "<br /><br />" + body;
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential
            ("", "");// Enter seders User name and password
            smtp.EnableSsl = true;
            smtp.Send(mail);

            TempData["ResultMessage"] = "Thank you for your enquiry and or enrolment. we will attend to this submission and come back to you soon";
            TempData["TimeMessage"] = "Your entry was received on"+DateTime.Now;
            return RedirectToAction("Index");
        }


        return View(contact_US);
    }

在这种情况下,如果模型状态无效,它会查看创建视图,但我不想拥有创建视图。我只是想联系我们到索引页面。

这是我的索引视图

        @using (Html.BeginForm("Create", "Contact_Us", FormMethod.Post, new { @id = "contactusform" }))
        {
            @Html.AntiForgeryToken()

            <div class="form-horizontal">



                <div class="form-group">
                    @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2 txtformat" })
                    <div class="col-md-12">
                        @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2 txtformat" })
                    <div class="col-md-12">
                        @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2 txtformat" })
                    <div class="col-md-12">
                        @Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group hidden">
                    @Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2 txtformat" })
                    <div class="col-md-12 contactusmsg">
                        @Html.TextAreaFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group contactuspostbtn">
                    <div class="col-md-12">
                        <input id="postcontactusmessage" type="submit" value="Send" class="btn btn-default" />
                    </div>

                </div>
            </div>
        }

感谢任何帮助

1 个答案:

答案 0 :(得分:2)

如果您只想调用视图并传入正确的模型,那么您可以使用带有视图名称的重载,如下所示:

使用型号加载视图:

return View("Index", contact_US); //Index is View name and contact_US is the Model.

加载不带模型的视图:

return View("Index");

确保视图所期望的模型以及您从该操作传递的内容是匹配的。

理想情况下,除了一些特定情况更好地保持接近MVC实践。例如:如果用户从“联系”页面提供了数据,那么将它们重新定向到“联系”页面而不是其他页面会很容易理解。这需要很多谨慎,特别是在重命名视图等时。

希望这对你有所帮助。