2

我正在研究 asp.net mvc 3 应用程序,我遇到了这个问题。我在强类型剃刀视图中工作,现在我编写了一个带有删除/上传选项的简单图片库。我遵循一个工作示例,其中更新和删除的逻辑位于Html.BeginForm. 我还有另一个表单助手,这是删除了一些代码的视图(我的意思是我认为与此问题无关的代码):

@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此处传递 as 参数:@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..

我看到这是从表单中获取值的一种方法,但我找不到传递它的方法。但是,如果您有其他想法,则没有问题。实施取决于我的决定。

4

2 回答 2

1

我找到的解决方案是这样的:

@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>
}

然后在我的控制器中进行了以下更改:

 public ActionResult Upload(FormCollection collection)
        {
            WebImage UploadImage = WebImage.GetImageFromRequest();
            long documentID;
            string finalImageName = null;
            if (!long.TryParse(collection.AllKeys[0], out documentID))

可能不是最好的方法,但迄今为止唯一可以完成这项工作的方法。

于 2013-05-30T05:29:43.183 回答
0
<input type="hidden" name="Id" value="@Model[0].Id"/><!--Add this line-->
if (ViewBag.Status == 1)
{
    <button type="submit" id="submitDocument">Save</button> 
    <button style="float:right;" id="finalizeDocument">Finish</button>  
}
else
{ 
    @Html.ActionLink("Назад", "Index")
}
于 2013-05-29T17:28:53.763 回答