我在开发 asp.net mvc 3 应用程序时遇到了这个问题。我在强类型 razor View 中工作,现在我编写了一个带有删除/上传选项的简单图片库。我遵循一个工作示例,其中更新和删除的逻辑在 Html.BeginForm
中。 .我还有另一个表单助手,这里是删除了一些代码的 View (我的意思是我认为与此问题无关的代码):
@model List<DataAccess.MCS_DocumentFields>
@{
ViewBag.Title = "Document";
}
<div id="alabala">
<div id="drawForm">
@using (Html.BeginForm("UpdateDocument", "Forms", FormMethod.Post))
{
<table border="1" id="drawDocument">
<colgroup>
<col span="1" style="width: 10%;" />
<col span="1" style="width: 40%;" />
<col span="1" style="width: 25%;" />
<col span="1" style="width: 25%;" />
</colgroup>
@Html.Partial("_PartialHeader", Model)
@Html.Partial("_PartialDrawing", Model)
@Html.Partial("_PartialBody", Model)
@Html.Partial("_PartialFooter", Model)
</table>
if (ViewBag.Status == 1)
{
<button type="submit" id="submitDocument">Save</button>
<button style="float:right;" id="finalizeDocument">Finish</button>
}
else
{
@Html.ActionLink("Назад", "Index")
}
}
</div>
<div id="imageContainer">
<div id="imageGallery" style="overflow: scroll">
<img src="file:\\10.3.12.237\MaritsaEast\upload_McsTemp\10005\docPicTest1.jpg" alt="docImg" style="width: 190px; height: auto"/>
<a href=#>Delete</a>
<img src="file:\\10.3.12.237\MaritsaEast\upload_McsTemp\10005\docPicTest2.jpg" alt="docImg" style="width: 190px; height: auto"/>
<a href=#>Delete</a>
<img src="file:\\10.3.12.237\MaritsaEast\upload_McsTemp\10005\docPicTest3.jpg" alt="docImg" style="width: 190px; height: auto"/>
<a href=#>Delete</a>
<img src="file:\\10.3.12.237\MaritsaEast\upload_McsTemp\10005\docPicTest4.jpg" alt="docImg" style="width: 190px; height: auto"/>
<a href=#>Delete</a>
<img src="file:\\10.3.12.237\MaritsaEast\upload_McsTemp\10005\docPicTest5.jpg" alt="docImg" style="width: 190px; height: auto"/>
<a href=#>Delete</a>
</div>
@using (Html.BeginForm(new { Id = 1005})
{
<input type="file" name="datafile" id="file" onchange="readURL(this);" />
<input type="button" name="Button" value="Качи" id="UploadButton" onclick="fileUpload(this.form,'/forms/upload','upload'); return false;"/>
<div id="upload" style="display: inline-block;">
<img id="blah" src="#" alt="your image" style="display:none;"/>
</div>
}
</div>
</div>
这个:@model List<DataAccess.MCS_DocumentFields>
保留同一文档的记录,所以我想要实现的是通过 Model[0].Id
作为这里的参数:@using (Html.BeginForm(new { Id = 1005})
.现在我正在努力编码只是为了让它工作,但如果同时实现这两个目标不是太困难,那么这篇文章将是完美的。这是 Upload
的开始我的方法 FormsController
:
public void Upload()
{
WebImage UploadImage = WebImage.GetImageFromRequest();
long DocumentID;
if (!long.TryParse(Request.Form["Id"], out DocumentID))
{
return;
}
//and more code follows..
我看到这是从表单获取值的一种方法,但我找不到传递它的方法。但是,如果您有其他想法,则没有问题。实现取决于我的决定。
最佳答案
我找到的解决方案是这样的:
@using (Html.BeginForm("Upload", "Forms", FormMethod.Post))
{
<input name=@Model[0].DocumentId type="hidden" />
<input type="file" name="datafile" id="file" onchange="readURL(this);" />
<input type="button" name="Button" value="Качи" id="UploadButton" onclick="fileUpload(this.form,'/forms/upload','upload'); return false;"/>
<div id="upload" style="display: inline-block;">
<img id="blah" src="#" alt="your image" style="display:none;"/>
</div>
}
然后在我的 Controller 中进行了以下更改:
public ActionResult Upload(FormCollection collection)
{
WebImage UploadImage = WebImage.GetImageFromRequest();
long documentID;
string finalImageName = null;
if (!long.TryParse(collection.AllKeys[0], out documentID))
可能不是最好的方法,但却是迄今为止唯一能做到这一点的方法。
关于c# - 从强类型 Razor View 使用 Html.BeginForm 传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16812972/