C# 通过提交按钮传递值?
我试图通过表单的提交按钮传递值 以下是我需要的值:C# 通过提交按钮传递值?,c#,javascript,asp.net-mvc,submit,C#,Javascript,Asp.net Mvc,Submit,我试图通过表单的提交按钮传递值 以下是我需要的值: [HttpPost] public ActionResult Upload() //string token, string filename, string moddate, object file { Dictionary<string, string> parameters = new Dictionary<string, string>();
[HttpPost]
public ActionResult Upload() //string token, string filename, string moddate, object file
{
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("Token", token);
parameters.Add("FileName", filename);
parameters.Add("ModDate", DateTime.Today.ToString());
parameters.Add("File", file);
String compleat = "Complete";
return View(compleat);
}
[HttpPost]
public ActionResult Upload()//字符串标记、字符串文件名、字符串moddate、对象文件
{
字典参数=新字典();
参数。添加(“令牌”,令牌);
添加(“文件名”,文件名);
parameters.Add(“ModDate”,DateTime.Today.ToString());
参数。添加(“文件”,文件);
字符串completat=“Complete”;
返回视图(completat);
}
这是我尝试获取值的地方:
<form action="/Home/Upload" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
@string token = @Model.Token;
@string fileName = file.tostring();
@File actualfile = file;
<br>
<input type="submit" name="submit" value="Submit" />
文件名:
@字符串标记=@Model.token;
@字符串文件名=file.tostring();
@文件实际文件=文件;
我想这样做,我的JavaScript可能是错误的,因为我是新手
单击提交后,如何从主控制器访问变量 在MVC中,您希望使用viewmodels。还可以使用html.beginform帮助程序,这样您的代码就不会看起来这么凌乱 上载ViewModel.cs
public class UploadViewModel
{
public string Token { get; set; }
public string FileName { get; set; }
public string ModDate { get; set; }
public object File { get; set; }
}
HomeController.cs
public ActionResult Upload()
{
TempData["Status"] = "";
return View(new UploadViewModel());
}
[HttpPost]
public ActionResult Upload(UploadViewModel upload) //string token, string filename, string moddate, object file
{
//*** Do something with the upload viewmodel
// It's probably a good idea to store the message into tempdata
TempData["Status"] = "Complete";
return View();
}
Upload.cshtml
@model UploadViewModel
@Html.Label(TempData["Status"].ToString())
@using (Html.BeginForm())
{
@Html.LabelFor(model => model.Token)
@Html.EditorFor(model => model.Token)
@Html.LabelFor(model => model.ModDate)
@Html.EditorFor(model => model.ModDate)
@Html.LabelFor(model => model.FileName)
@Html.EditorFor(model => model.FileName)
<input type="submit" name="submit" value="Submit" />
}
@model UploadViewModel
@Html.Label(TempData[“Status”].ToString())
@使用(Html.BeginForm())
{
@LabelFor(model=>model.Token)
@EditorFor(model=>model.Token)
@LabelFor(model=>model.ModDate)
@EditorFor(model=>model.ModDate)
@LabelFor(model=>model.FileName)
@Html.EditorFor(model=>model.FileName)
}
这是非常基本的东西,你应该读一些教程。例如: