【问题标题】:MVC4 Call back from server to clientMVC4 从服务器回调到客户端
【发布时间】:2015-06-15 06:31:56
【问题描述】:

我正在使用 ASP.NET MVC 4 应用程序,我需要通过从控制器向客户端发送消息来在客户端中显示消息。

我需要将文件上传到服务器并在 Foreach 循环中进行一些处理,并且每次 foreach 我需要在 UI 中显示消息。 目前我有for循环,在这种情况下,我需要在每个for循环上从服务器向客户端发送消息

查看

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "formUpload", enctype = "multipart/form-data" }))
{
    <div>
        <b>Upload File</b>
        <input type="file" name="file" />
        <input type="submit" value="Upload File" name="btnUpload" onclick="progressStatus();"/><br />
    </div>
    <div>
        @ViewBag.Message
    </div>
    <div style="width: 30%; margin: 0 auto;">
        <div id="progressbar" style="width: 300px; height: 15px"></div>
        <br />
    </div>
}

控制器代码

[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
    if (file != null)
    {
        var fname = Path.GetFileName(file.FileName);
        var exis = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/Storage/uploads"), fname);
        if (System.IO.File.Exists(exis))
        {
            ViewData["Message"] = "The file " + fname + " has already exists";
        }
        else
        {
            try
            {
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var folderPath = Server.MapPath("~/Storage/uploads");
                    fname = fileName;
                    var path = Path.Combine(folderPath, fileName);
                    var filebytes = new byte[file.ContentLength];
                    if (!Directory.Exists(folderPath))
                        Directory.CreateDirectory(folderPath);
                    file.SaveAs(path);
                    for (int i = 0; i < 20; i++)
                    {
                        //Display This Message in UI From here each time for runs like i want to show user message 1,2,3,4 etc each time for runs
                    }
                }
                ViewData["Message"] = "The file " + fname + " has uploaded successully";
            }
            catch (Exception e)
            {
                ViewData["Message"] = "The file " + fname + " Could not upload";
                ViewData["Message"] = e.Message;
            }
        }
    }
    else
        ViewData["Message"] = "Please choose file";
                return View();
}

【问题讨论】:

  • 初始化 List&lt;string&gt; 并向其发送消息,然后将其分配给 ViewBag(或者最好是视图模型)属性,以便您可以在视图中进行迭代。
  • 如果您正在查看客户端浏览器的on the fly 进度消息,那么您必须使用SignalR 之类的东西。使用传统的 ASP.Net MVC api,您无法将通知推送到客户端浏览器。
  • 我是 MVC 4 的新手,你能帮我怎么做吗,比如 HTTP 响应消息
  • 我建议使用ajax循环并在服务器中执行与该循环相对应的操作,同时您可以将消息显示给客户
  • Vivekh 你能告诉我怎么做吗

标签: c# asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

您可以在循环的每次迭代中创建字典键值对,例如

for (int i = 0; i < 20; i++)
                {
                    ViewData["Message_"+i.ToString()] = //your message;
                }

【讨论】:

  • 每次运行时我都需要返回查看如何实现
  • 这是一个奇怪的要求,为什么你不能把for循环也放在视图中并使用索引访问。
  • 我是 MVC 新手,所以 -> 我的要求是,一旦我将文件保存在某个位置,我需要像在 for 循环中那样处理文件,因为我可能会运行 for 循环 10 次或基于某些参数 20 次,我想将每个 for 循环的消息返回到 UI
  • @BharatReddy,您无法在每次迭代中返回视图。一旦您返回视图,您就退出该方法!
  • 我的代码-drive.google.com/…
【解决方案2】:
You can use `StringBuilder`:

    var sb = new StringBuilder();      

    for (int i = 0; i < 20; i++)
    {
         bool isValid = doSomeThing();

         if (isValid)
         {
            sb.Append("<li>Loop success : " + i + "</li>");     
         }
         else
         {
            sb.Append("<li>Error in loop : " + i + "</li>");         
          }

      }

    viewBag.msg = sb.toString();

【讨论】:

  • 我需要将消息从客户端推送到服务器如何做到这一点.. 就像进度文件处理和消息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-13
  • 1970-01-01
  • 2012-06-22
  • 2018-04-13
相关资源
最近更新 更多