0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ZpfSysn
  • 807
  • 2
  • 12
  • 30
  • Is there a particular reason you're using ASPX, when the popular (and IMO far superior) view engine for MVC is Razor? – mason Feb 19 '19 at 20:18
  • @mason i don't have a say for this. This is a existing project that I(as intern) suppose to work on. – ZpfSysn Feb 19 '19 at 20:19
  • Ouch. Well my condolences to you. I hope you don't go forward thinking that ASPX (the Web Forms View Engine) is representative of the quality of the framework as a whole. It gets better. – mason Feb 19 '19 at 20:22
  • @mason this is actually good to hear. I really don't like ASPX. Glad to hear there are better choices. – ZpfSysn Feb 19 '19 at 20:38
  • 1
    change public ActionResult SMS(string supplierId) to public ActionResult SMS(string phone) and do var model = new _2FASMSModel() {Phone = phone} before returning – Karthik Ganesan Feb 19 '19 at 20:47
  • @KarthikGanesan thanks, that worked too! – ZpfSysn Feb 19 '19 at 20:55

2 Answers2

1

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");
}
J.P.
  • 5,567
  • 3
  • 25
  • 39
  • For the last line,`return RedirectToAction("SMS", "TwoFA", )`, is there another paremeter? since it seems like it. – ZpfSysn Feb 19 '19 at 20:28
  • Nope. Forget to remove that. Updated my answer :-). I also now only store a string in tempdata instead of an anonymous object, the object was not needed. – J.P. Feb 19 '19 at 20:29
  • The error occured at line: `var model = new _2FASMSModel() { Phone = TempData["Phone"] };` Apparently it treats `TempData["Phone"]` as object and it sais that cannot implicitly convert type object to string – ZpfSysn Feb 19 '19 at 20:37
0
[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" })

Alik
  • 5
  • 2