Reputation: 15197
I am trying to pass values thought the submit button of a form.
These are the values i need:
[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);
}
This is where i try get the values:
<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" />
I would like to do something like this, my JavaScript is probably wrong as i am new to it.
After the submit is clicked how can i access the variables from the home controller?
Upvotes: 0
Views: 1029
Reputation: 2126
In MVC you want to work with viewmodels. There is also html.beginform helper to use so your code won't look so messy.
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" />
}
This is pretty basic stuff, you should read up some tutorials. Like these: http://www.asp.net/mvc/tutorials
Upvotes: 2