Reputation: 9866
I'm working on asp.net mvc 3 application and I have this issue. I'm working inside strongly type razor view and now I write a simple image gallery with delete/upload options. I follow a working example where the logic for update and delete is inside a Html.BeginForm
. I have another form helper in my as well, here is the view with some of the code removed (I mean the code that I think is irrelevant to this issue) :
@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>
This : @model List<DataAccess.MCS_DocumentFields>
hold a records for the same document so what I want to achieve is pass the Model[0].Id
as parameter here : @using (Html.BeginForm(new { Id = 1005})
. For now I'm hard coding it just to make it work, but if it's not too difficult to achieve both at the same time with this post will be perfect. This is the begining of the Upload
method in my FormsController
:
public void Upload()
{
WebImage UploadImage = WebImage.GetImageFromRequest();
long DocumentID;
if (!long.TryParse(Request.Form["Id"], out DocumentID))
{
return;
}
//and more code follows..
I saw that this is one way to get a value from a form but I can't find a way to pass it. However if you have another idea there is no problem. The implementation is up to what I decide it will be.
Upvotes: 2
Views: 3147
Reputation: 9866
The solution I found is this :
@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>
}
and then in my controller made this changes :
public ActionResult Upload(FormCollection collection)
{
WebImage UploadImage = WebImage.GetImageFromRequest();
long documentID;
string finalImageName = null;
if (!long.TryParse(collection.AllKeys[0], out documentID))
Probably not the best way to do it but the only one that do the job till now.
Upvotes: 1
Reputation: 194
<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")
}
Upvotes: 0