2012-09-03 127 views
0

我想通過值的思想提交按鈕的窗體。通過提交按鈕傳遞值?

這些都是我需要的值:

[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);    
     } 

這是我嘗試獲取值:

<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" /> 

我願做這樣的事情,我的JavaScript是可能是錯誤的,因爲我是新手。

點擊提交後,我如何訪問家庭控制器的變量?

回答

2

在MVC中,您希望使用viewmodels。還有html.beginform助手可以使用,所以你的代碼看起來不會太雜亂。

UploadViewModel.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" /> 
} 

這是很基本的東西,你應該閱讀一些教程。像這些:http://www.asp.net/mvc/tutorials