简体   繁体   English

Mvc视图渲染

[英]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: 那将是您的Controller,然后是该特定页面的Controller,它看起来像:

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. 但是,您依赖于Post,这将导致屏幕闪烁。 So the better route, may be to do Ajax, then return a JsonResult . 因此,更好的方法可能是先执行Ajax,然后返回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. 问题是因为我正在使用sitecore cms,因此表单操作没有处理它的全部工作流程,所以在删除操作后,它默认为在cms中定义并触发cms工作流程的action方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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