简体   繁体   中英

Mvc View rendering

I'm trying to create contact us page where user fill's in the detail and submit and at the bottom display message which comes from server.

The way i have implemented is something like this.

[HttpGet]
public ActionResult ContactUs()
{
    //Process the stuff
    return View("~Views/Contact/Contact.cshtml", model)
}

now when page load it hits above method and display form with the layout including header and footer.

Once user submits form it hits below method

[HttpPost]
public ActionResult ContactUs(ContactUs form)
{
    //Process the stuff

    View.Message="Thank you for your enquiry."

    return View("~Views/Contact/Contact.cshtml", model)
}

It returns to the same page but it doesnt render the body layout not even header or footer simply display outputs form.

Not sure what im doing wrong there, is there any better approach ?

Thanks

Based on the code above, I believe you're attempting something like:

public class UxController : Controller
{
     public ActionResult WithResponse(ActionResult result, string message)
     {
          PageResponse(message);
          return result;
     }

     protected void PageResponse(string message)
     {
          TempData["Ux_Response"] = message;
     }
}

That would be your Controller, then the Controller for that specific page, it would look like:

public class HomeController : UxController
{
     public ActionResult Index()
     {
           return View();
     }

     public ActionResult SubmitForm(string message)
     {
          return WithResponse(RedirectToAction("Index"), "Thank you for feedback.");
     }
}

Then in your front-end code, you would do the following:

@if(TempData["Ux_Response"] != null)
{
     <div>@TempData["Ux_Response"]</div>
}

<form action="/Home/SubmitForm" method="post">
     <input type="text" name="message" />
     <input type="submit" value="Submit" />
</form>

Obviously you could enhance this, with more versatility. However, you're relying on Post, which will cause a screen flicker. So the better route, may be to do Ajax, then return a JsonResult . Hopefully this helps you out.

It should work if you change your controller/view like this.

Controller;

    public ActionResult Contact(ContactModel model)
    {
        ViewBag.Message = "Your contact page.";

        return View(model);
    }


    public ActionResult SaveContact(ContactModel model)
    {
        //process values in your model and then rest model
        ContactModel.Message = "Thank you for contacting us"; //show thank you message

        return RedirectToAction("Contact",model);
    }

View;

@model MvcApplication1.Models.ContactModel 
@{
    ViewBag.Title = "Contact";
}

@using (Html.BeginForm("SaveContact", "Home", Model, FormMethod.Post))
{
    @Html.DisplayFor(m => m.Message);

    <button type="submit">Submit</button>
}

I manged to solve this. the issue was the because i was using sitecore cms the form action wasnt processing it full work flow, after i removed the action, it defaults to action method which defined in cms and triggers the cms workflow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM