繁体   English   中英

Mvc视图渲染

[英]Mvc View rendering

我正在尝试创建与我们联系的页面,用户在其中填写详细信息并提交,并在底部显示来自服务器的消息。

我实现的方式是这样的。

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

现在,当页面加载时,它会击中上述方法并显示包含页眉和页脚的布局表格。

用户提交表单后,将点击以下方法

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

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

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

它返回到同一页面,但它不呈现正文布局,即使页眉或页脚也不仅仅显示输出表单。

不知道我在哪里做错了,有没有更好的方法?

谢谢

根据上面的代码,我相信您正在尝试类似的事情:

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

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

那将是您的Controller,然后是该特定页面的Controller,它看起来像:

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

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

然后,在您的前端代码中,您将执行以下操作:

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

显然,您可以通过更多功能来增强此功能。 但是,您依赖于Post,这将导致屏幕闪烁。 因此,更好的方法可能是先执行Ajax,然后返回JsonResult 希望这可以帮助您。

如果这样更改控制器/视图,它应该可以工作。

控制器;

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

视图;

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

我努力解决这个问题。 问题是因为我正在使用sitecore cms,因此表单操作没有处理它的全部工作流程,所以在删除操作后,它默认为在cms中定义并触发cms工作流程的action方法。

暂无
暂无

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

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