繁体   English   中英

从Html.BeginForm从强类型的Razor视图传递值

[英]Passing value with Html.BeginForm from a strongly typed Razor view

我正在处理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作为参数: @using (Html.BeginForm(new { Id = 1005})现在,我正在努力对其进行编码,以使其正常工作,但是如果通过这篇文章同时实现两者并不难,那么这将是完美的。这是我FormsController Upload方法的开始:

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

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

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

也许这不是最好的方法,但是到目前为止,只有这一种方法可以完成。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM