2013-05-29 37 views
2

我正在處理asp.net mvc 3應用程序,並且遇到此問題。我正在使用強類型剃刀視圖,現在我編寫了一個帶有刪除/上載選項的簡單圖像庫。我遵循一個工作示例,其中更新和刪除的邏輯位於Html.BeginForm之內。我有我的另一種形式的幫助爲好,這裏是一些刪除代碼(我的意思是,我認爲是無關緊要的對這一問題的代碼)的觀點:從強類型剃刀視圖中傳遞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作爲參數在這裏:@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.. 

我看到,這是獲得來自表單的值的一種方式,但我不能找到一種方法來傳遞。但是,如果你有另一個想法,沒有問題。實施取決於我的決定。

+0

你的意思是你不能在post方法中獲得'Id'的值嗎? –

+0

我無法傳遞價值。我不確定如何將它傳遞給我的控制器,我必須得到它。 – Leron

回答

1

我找到了解決辦法是這樣的:

@using (Html.BeginForm("Upload", "Forms", FormMethod.Post)) 
{ 

    <input [email protected][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)) 

大概沒有做到這一點的最好辦法,但只有一個直到現在完成這項工作。

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") 
} 
+0

那麼,唯一的問題是,這是從另一種形式的一部分,我沒有從這種形式和其他形式的數據職位之間的關係。不管怎麼說,多謝拉。 – Leron

相關問題