- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 MVC 编码的初学者。
当应用程序启动时,ViewBag.Message 是:选择要上传的文件。
上传成功后,变为:文件上传成功!
有没有办法让它在大约 5 秒后返回并再次显示“选择要上传的文件”消息,而不使用任何 javascript? 我想如果 mvc 有一些我可以使用的内置时间函数?
https://github.com/xoxotw/mvc_fileUploader
我的看法:
@{
ViewBag.Title = "FileUpload";
}
<h2>FileUpload</h2>
<h3>Upload a File:</h3>
@using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, new {enctype = "multipart/form-data"}))
{
@Html.ValidationSummary();
<input type="file" name="fileToUpload" /><br />
<input type="submit" name="Submit" value="upload" />
@ViewBag.Message
}
我的 Controller :
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Mvc_fileUploader.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Choose a file to upload!";
return View("FileUpload");
}
[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase fileToUpload)
{
if (ModelState.IsValid)
{
if (fileToUpload != null && fileToUpload.ContentLength > (1024 * 1024 * 1)) // 1MB limit
{
ModelState.AddModelError("fileToUpload", "Your file is to large. Maximum size allowed is 1MB !");
}
else
{
string fileName = Path.GetFileName(fileToUpload.FileName);
string directory = Server.MapPath("~/fileUploads/");
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
string path = Path.Combine(directory, fileName);
fileToUpload.SaveAs(path);
ModelState.Clear();
ViewBag.Message = "File uploaded successfully!";
}
}
return View("FileUpload");
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
最佳答案
简短的回答是否。我猜是因为你是“新手”,所以你想专注于 MVC 部分,但 MVC 和 JavaScript 是非常相互关联的,想想客户端 (JavaScript) 和服务器 (MVC),你应该真正掌握两者才能制作出好的网站。
通常服务器不会向浏览器发送事件,而是由浏览器发出请求。有一些方法可以让服务器使用诸如 SignalR 之类的东西在客户端上引发事件但在这种情况下,这就太过分了。
最后...您要实现的很大程度上是客户端操作,即通知用户做某事。如果您在 MVC 中这样做,您会浪费网络带宽并增加延迟(认为服务器调用很昂贵),而实际上它是一个客户端操作,因此应该在 JavaScript 中完成。
不要回避 JavaScript。拥抱它。查看JQuery这为您减轻了很多繁重的工作。
关于c# - MVC-4 在服务器端更改 ViewBag.Message?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16561215/
我需要将一些信息传递给部分 cshtml View 。为此,我使用了 ViewDataDictionary 和 ViewBag。 public ActionResult TaskPage(int ID
我有 RedirectToAction 作为 Controller 中的返回值,在我看来我使用 ViewBag。虽然在 RedirectToAction 之后 ViewBag 变空是一个众所周知的问题
我已经为 HtmlHelper 编写了扩展方法,该方法是从需要访问 ViewBag 以获取页面设置的信息的母版页调用的。 这是方法签名: public static string BuildFavor
我正在尝试在我的应用程序中使用 ViewBag,我拥有所有最近的 dll、最新版本的 MVC 3,但我仍然收到错误: "The name 'ViewBag' does not exist in the
我正在尝试通过 JavaScript 将 ViewBag 中的值插入到输入中并动态创建该值。 我调用 Controller 创建的employeeID的值 public string GetEmplo
我需要显示存储在 ViewBag 中的日期,为此我需要使用 DisplayTemplate。现在,如果它是模型中的日期,我会使用 DisplayFor 助手,但我该如何为 ViewBag 做呢? 我有
我知道网上已经有一些解决方案,但在尝试了所有解决方案后,我对自己感到困惑。 我只想澄清我尝试过的解决方案: 确保 Microsoft.AspNet.Web.Optimization 或 System.
在MSDN documentation ,有 ViewBag.MyMessageToUsers 和 ViewBag.AnswerText。在ASP.net/mvc tutorial , 有 ViewB
这个问题在这里已经有了答案: Can the ViewBag name be the same as the Model property name in a DropDownList? (2 个答
我测试了我的应用程序,在测试时我发现了一个错误,我认为它来自 ViewBage。我有 500 名医生和专科医生的名单,当我想编辑电话、地址等时,下拉菜单会显示列表中第一个专科医生的名字,所以它不会保存
我有一个在数据库中注册聊天的表单,一切正常,问题是 ViewBag,因为这会将消息带到 View 中的 Javascript,以便它可以进行验证。当用户注册时,应该会出现一条成功消息,但在 ViewB
有没有办法清除ViewBag? ViewBag 没有 setter,因此不能简单地取消它: ViewBag = null; 我似乎也不能use reflection迭代它并清除它的动态属性,因为您不能
想象一个名为的 View 包 ViewBag.Modes 这包含以下内容: Simple Advanced Manual Complete 如何像在数组中那样通过索引访问 viewbag? 例如 S
是否可以在调用重定向之前设置 ViewBag? 我想要这样的东西: @ViewBag.Message="MyMessage"; RedirectToAction("MyAction"); 最佳答案 当
我正在做一个项目,我只需要从两个表中获取数据,我想使用 LINQ Query 来获取数据。 在我的 Controller 中: var Parameters = (from test in _db.t
与'12 asp.net C# MVC4 - Int.Appl.Template EF Code First 这是我非常简单的脚本 $('#Add').click(function (e)
这是我的 Controller 代码: const string template = "clm_adj_process_cd ('@[CdCode]',varchar) ~f.src_dm_plat
我在 MVC3 项目中有一个 View ,在加载时我将一个实体设置到 ViewBag 中。 实体的属性之一是 Datetime? 类型。 在 $(document).ready我将 ViewBag 中
我尝试将 ViewBag.OrderId 设置为数据属性 data-orderId 的值,但在 =Add } JS: $(".btn-add-note").click(function(){
我需要显示存储在 ViewBag 中的日期,并且我需要使用 DisplayTemplate 来实现此目的。现在,如果它是模型中的日期,我将使用 DisplayFor 助手,但我该如何为 ViewBag
我是一名优秀的程序员,十分优秀!