gpt4 book ai didi

c# - 通过提交按钮传递值?

转载 作者:行者123 更新时间:2023-12-02 19:23:49 24 4
gpt4 key购买 nike

我试图通过表单的提交按钮传递值。

这些是我需要的值:

[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 可能是错误的,因为我是新手。

单击提交后,如何从家庭 Controller 访问变量?

最佳答案

在 MVC 中,您想要使用 View 模型。还有 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();
}

上传.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

关于c# - 通过提交按钮传递值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12245190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com