我正在尝试将此Web表单应用程序转换为我的MVC应用程序,这里是新手

时间:2019-05-09 05:06:48

标签: asp.net asp.net-mvc webforms

我正在尝试将此Webform应用程序转换为MVC应用程序

如何查看控制器的列表框?

我是新手

这是Webform:

protected void btnPrintToken_Click(object sender, EventArgs e)
{
        Queue<int> tokenQueue = (Queue<int>)Session["TokenQueue"];
        lblStatus.Text = "There are" + tokenQueue.Count.ToString()+
        " customers before you in the queue;

        if (Session["LastTokenNumberIssued"] == null)
            Session["LastTokenNumberIssued"] = 0;
        }
        int nextTokenNumberTobeIssued = (int)Session["LastTokenNumberIssued"] + 1;
        Session["LastTokenNumberIssued"] = nextTokenNumberTobeIssued;
        tokenQueue.Enqueue(nextTokenNumberTobeIssued);
        foreach(int token in tokenQueue)
        listTokens.Item.Clear();
        {
            listTokens.Items.Add(token.ToString());
        }
}

这是我的MVC应用程序:

    public ActionResult PrintToken()
    {
        Queue<int> tokenQueue = (Queue<int>)Session["TokenQueue"];

        if (Session["LastTokenNumberIssued"] == null)
        {
            Session["LastTokenNumberIssued"] = 0;
        }

        int nextTokenNumberTobeIssued = (int)Session["LastTokenNumberIssued"] + 1;
        Session["LastTokenNumberIssued"] = nextTokenNumberTobeIssued;

        tokenQueue.Enqueue(nextTokenNumberTobeIssued);

        foreach(int token in tokenQueue)
        {
            listTokens.Items.Add(token.ToString());
        }

        return View();
    }

它几乎具有相同的视图,但是我不知道如何像在“ lblStatus.Txt”之类的网络表单中一样在MVC中访问我的视图

3 个答案:

答案 0 :(得分:2)

使用MVC,您无需直接与HTML元素进行交互,而是提供视图模型(或者,您可以使用ViewBag)并使用Razor syntax动态呈现视图。

以下是将视图模型添加到PrintToken()操作并呈现消息的步骤:

步骤1):创建一个视图模型,该模型代表要在视图上显示的所有动态值:

public class SampleViewModel
{
  public string StatusMessage { get; set; }
}

步骤2)实例化视图模型的实例,填充属性并返回视图:

public ActionResult PrintToken()
{
    // Create instance of your view model
    var viewModel = new SampleViewModel();

    // Somewhere within your code set the StatusMessage
    viewModel.StatusMessage =  $"There are {tokenQueue.Count.ToString()} customers before you in the queue";

    // Return the view model with the view
    return View(viewModel);
}

第3步):在视图中指定视图模型,然后渲染属性:

<!-- Declare the model on the very top (first line) of your view -->
<!-- Note: This should be the full path including namespace -->
@model SampleViewModel

<!-- Render the property from the view-->
<p>@Model.StatusMessage</p>

答案 1 :(得分:1)

您可以根据需要使用ViewBag或ViewData存储消息

public ActionResult PrintToken()
    {
        Queue<int> tokenQueue = (Queue<int>)Session["TokenQueue"];

       ViewBag.StatusText = "There are" + tokenQueue.Count.ToString()+
        " customers before you in the queue;

        if (Session["LastTokenNumberIssued"] == null)
        {
            Session["LastTokenNumberIssued"] = 0;
        }

        int nextTokenNumberTobeIssued = (int)Session["LastTokenNumberIssued"] + 1;
        Session["LastTokenNumberIssued"] = nextTokenNumberTobeIssued;

        tokenQueue.Enqueue(nextTokenNumberTobeIssued);

        foreach(int token in tokenQueue)
        {
            listTokens.Items.Add(token.ToString());
        }

        return View();
    }

在您的cshtml文件中

<label>@ViewBag.StatusText</label>

您还可以将ViewModel创建为

class PrintTokenViewModel{
     public List<string> listTokens{get;set;}
     public string StatusText{get;set;}
}

其他解决方案:使用ViewModel存储数据

在控制器中

public ActionResult PrintToken()
 {
     var model = new PrintTokenViewModel();
     //handle assign here
     return View(model);
}

在View cshtml中

@model PrintTokenViewModel

<ul>
@foreach(var item in Model.ListTokens){
   <li>@item</li>
}
</ul>

<label>@Model.StatusText</label>

答案 2 :(得分:0)

您可以将数据保存在ViewModel中,然后通过Controller将其传递给视图,如下所述

public ActionResult PrintToken()
{
      PrintViewModel print = new PrintViewModel();

    return View(print);
}

PrintToken.cshtml
@model  SampleMVC.Models.PrintViewModel
@{
    ViewBag.Title = "PrintToken";
}

<h2>PrintToken</h2>