-2

呈现输入值文件名并将其发送到控制器的方法有哪些:

<div id="fileuploaddiv" class="fileuploaddivclass">
<form action="@Model.FormAction" method="@Model.FormMethod"
    enctype="@Model.FormEnclosureType">
<input type="hidden" name="key" value="uploads/${filename}" id="filename" />
<input type="hidden" name="AWSAccessKeyId" value="@Model.AWSAccessKey" />
<input type="hidden" name="Content-Type" value="image/jpeg">
<div>
    Please specify a file, or a set of files:
    <input type="file" name="file" />
</div>
<input type="submit" value="Upload" />
</form>
</div>
4

1 回答 1

1

您需要查找一些 MVC3 约定(我建议将 NerdDinner 作为一个很好的入门教程),但这里有一个与您想要做的有点相似的方法:

@Model YourViewModel
<div id="fileuploaddiv" class="fileuploaddivclass">
    @using(Html.BeginForm(Model.FormAction, Model.FormController, FormMethod.Post)
        @Html.HiddenFor(model.key => ${fileName})
        @Html.HiddenFor(model.AWSAccessKeyID)
        @Html.HiddenFor(model.Content-Type)
        @<input type="submit" value="Submit My Form" />
    @Html.EndForm()
</div>

您的模型看起来像(我在这里很困惑,因为您似乎在动态设置控制器和操作,这很不寻常):

public class YourViewModel
{
     public string FormAction { get; set; }
     public string FormController { get; set; }
     public int AWSAccessKeyID { get; set; }
     public string Content-Type { get; set; }
}

现在到控制器:

[HttpGet]
public ActionResult WhateverControllerName()
{
    YourViewModel yvm = new YourViewModel();
    //Initalize viewmodel here
    Return view(yvm);
}

[HttpPost]
public ActionResult WhateverControllerName(YourViewModel yvm)
{
    if (ModelState.IsValid) {
        //Do whatever you want here. Perhaps a redirect?
    }
    return View(yvm);
}

注意:我的语法很垃圾,所以你必须检查一下,但 Visual Studio 应该告诉你什么是有效的。

于 2012-07-09T22:03:54.283 回答