简体   繁体   English

从控制器获取价值以在ASP.NET MVC中进行查看

[英]Getting value from controller to view in ASP.NET MVC

I am trying to get data passed from controller to view. 我正在尝试将数据从控制器传递到视图。 I believe I am really close, but I am missing some tiny piece. 我相信我真的很亲密,但是我缺少一些细小的东西。

Controller: 控制器:

namespace ePolicy.ConsumerPortal.Controllers
{
    [HandleErrors]
    public class TwoFAController : BaseController
    {
        [AcceptVerbs(new string[1] { "GET" })]
        public ActionResult SMS(string supplierId)
        {
            var model = new _2FASMSModel();
            return View("TwoFA_sms", model);
        }

        [AcceptVerbs(new string[1] { "POST" }), ValidateInput(false)]
        public ActionResult Initiate(FormCollection formValues, string email, string phone, string method)
        {
            return RedirectToAction("SMS", "TwoFA", new { phone = "1234567890"})
        }
    }
}

The model class: 模型类:

public class _2FASMSModel : BaseModel
{
    public string Phone { get; set; }

    public _2FASMSModel()
    {
    }
}

The view ( .aspx file) 视图( .aspx文件)

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/ConsumerPortalNew.Master" Inherits="System.Web.Mvc.ViewPage<ePolicy.ConsumerPortal.Models._2FASMSModel>" Debug="true" %>
<%@ Import Namespace="ePolicy.Resources" %>
<%@ Import Namespace="ePolicy.Shared.Enumeration" %>
<%@ Import Namespace="System.Web" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
</asp:Content>

<asp:Content ID="login" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript" src="<%: Url.StaticFile("/Scripts/TwoFA.js")%>"></script>
    <div>
       <p>@Model.Phone</p>
    </div>
</asp:Content>

What I wanted to do: I want to display the string "1234567890" in my view file. 我想做的事情:我想在视图文件中显示字符串“ 1234567890”。

What I have tried : I was able to make "1234567890" part of URL parameter, however, I was not able to retrieve this string so I can display in my view. 我尝试过的操作 :我能够使“ 1234567890”成为URL参数的一部分,但是,我无法检索此字符串,因此无法在视图中显示。

@Model.Phone will be interpreted as literal string instead of the value ("1234567890") that I wanted it to be. @Model.Phone将被解释为原义字符串,而不是我想要的值(“ 1234567890”)。

I also tried to use the ViewBag by adding this line 我还尝试通过添加以下行来使用ViewBag

ViewBag.Phone = supplierID

before returning a view. 在返回视图之前。 and calling it in view: 并在视图中调用它:

<p>ViewBag.Phone</p>

It did not work either. 它也不起作用。

Any help is appreciated. 任何帮助表示赞赏。

I believe you need to change: 我相信您需要更改:

<p>@Model.Phone</p>

to: 至:

<p><%: Model.Phone %></p>

And don't forget to actually fill your model with data: 并且不要忘记用数据实际填充模型:

[AcceptVerbs(new string[1] { "GET" })]
public ActionResult SMS(string supplierId)
{
    var model = new _2FASMSModel() { Phone = "HTC 10" };
    return View("TwoFA_sms", model);
}

That should at least display some dynamic data on your view. 那至少应该在您的视图上显示一些动态数据。 The last step is to pass on data from one controller action to the other. 最后一步是将数据从一个控制器动作传递给另一个控制器动作。 In ASP.NET MVC, there's a TempData property on your controller you can use. 在ASP.NET MVC中,您可以使用控制器上的TempData属性。 It's a dictionary where you can literally store a bit of data that will be available on the next request. 这是一个字典,您可以在其中存储一些数据,这些数据将在下一个请求中提供。 It seems that's exactly what you want. 看来这正是您想要的。

[AcceptVerbs(new string[1] { "GET" })]
public ActionResult SMS(string supplierId)
{
    var model = new _2FASMSModel() { Phone = TempData["Phone"] as string };
    return View("TwoFA_sms", model);
}

[AcceptVerbs(new string[1] { "POST" }), ValidateInput(false)]
public ActionResult Initiate(FormCollection formValues, string email, string phone, string method)
{
    TempData["Phone"] = "1234567890";
    return RedirectToAction("SMS", "TwoFA");
}
[AcceptVerbs(new string[1] { "GET" })]

public ActionResult SMS(string supplierId){ var model = new _2FASMSModel(); public ActionResult SMS(string providerId){var model = new _2FASMSModel();

model.Phone = "1234567890"; model.Phone =“ 1234567890”;

return View("TwoFA_sms", model); 返回View(“ TwoFA_sms”,model); } }

View 视图

@Html.LabelFor(model => model.Phone , htmlAttributes: new { @class = "control-label col-md-2" }) @Html.EditorFor(model => model.Phone , new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Phone , "", new { @class = "text-danger" }) @ Html.LabelFor(model => model.Phone,htmlAttributes:new {@class =“ control-label col-md-2”})@ Html.EditorFor(model => model.Phone,new {htmlAttributes = new {@ class =“ form-control”}})@ Html.ValidationMessageFor(model => model.Phone,“”,new {@class =“ text-danger”})

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

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