简体   繁体   English

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

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

I'm working on asp.net mvc 3 application and I have this issue. 我正在处理asp.net mvc 3应用程序,但遇到了这个问题。 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 . 我跟随一个工作示例,其中更新和删除的逻辑位于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 : 这: @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..

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. 实现取决于我决定的情况。

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. 也许这不是最好的方法,但是到目前为止,只有这一种方法可以完成。

<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