Search code examples
asp.net-mvc

how to pass an object to specific view with ASP.net MVC


I have a page which has a form and everything works correctly. My question is for when my model state is not valid, then how to return contact us to index view not create view?

 [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);
    }

in this situation if model state is not valid it looks for create view but I don't want to have a create view. I just want to bring contact us to index page.

this is my index view

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

Appreciate any help


Solution

  • If you just want to call a view and pass in correct model then you can use overload with view name as in below cases:

    Load view with Model:

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

    Load view without Model:

    return View("Index");
    

    Make sure the Model expected by the view and what you are passing from the action are matching.

    Ideally, except some specific cases better to stay close to MVC practices. Ex: If user provided data from Contact page then it would be nice and easily understood to redirect them back to Contact page instead of some other page. This need lot of caution especially while renaming the views etc.

    Hope this help you.