ZpfSysn
ZpfSysn

Reputation: 889

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)

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

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.

@Model.Phone will be interpreted as literal string instead of the value ("1234567890") that I wanted it to be.

I also tried to use the ViewBag by adding this line

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.

Upvotes: 0

Views: 104

Answers (2)

Alik
Alik

Reputation: 5

[AcceptVerbs(new string[1] { "GET" })]

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

model.Phone = "1234567890";

return 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" })

Upvotes: 0

J.P.
J.P.

Reputation: 5745

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

Upvotes: 1

Related Questions