如何在 asp.net mvc 中使用 Html.BeginForm() 发送多个参数

本文关键字:BeginForm 参数 Html asp net mvc | 更新日期: 2023-09-27 18:37:19

实际上我想将文本框中的图像和文本发送到控制器...

@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
         @Html.Label("UserName:") <input type="text" id="txtImg" name="txtImg" /><br /><br />
            @Html.Label("Upload:")<input type="file" name="image" /><br/>    
        <div id="Preview">
        Preview
    </div>
        <input class="btn btn-success btnUpload" type="submit" value="upload" />
    }

我正在尝试通过以下方式在控制器中检索它们:

 public ActionResult Upload(HttpPostedFileBase image,string txtImg)

但我没有得到文本框值。代码有什么问题吗?

我有这样的示例代码。

Controller:
    public ActionResult Upload()
            {
                BlobStorageServices _blobstorageservice = new BlobStorageServices();
                CloudBlobContainer container = _blobstorageservice.GetCloudBlobContainer();
                List<string> blobs = new List<string>();
                //List<BlobModel> models = BlobManager.getBlobs();
                foreach (var blobItem in container.ListBlobs())
                {
                    blobs.Add(blobItem.Uri.ToString());
                }
                return View(blobs);
            }
            [HttpPost]
            public ActionResult Upload(string txtImg,HttpPostedFileBase image)
            {
                if (image.ContentLength > 0)
                {
                    BlobStorageServices _blobstorageservice = new BlobStorageServices();            
                    CloudBlobContainer container = _blobstorageservice.GetCloudBlobContainer();
                    CloudBlockBlob blob = container.GetBlockBlobReference(image.FileName);
                 //BlobManager.insertBlobUri(new BlobImage { Uri = blob.Uri.ToString() });
                // string text = model.Name;
                  BlobManager.insertBlobUri(new BlobModel {Name=txtImg,Uri=blob.Uri.ToString()});
                    blob.UploadFromStream(image.InputStream);
                }
                return RedirectToAction("Upload");
            }
    View
    @using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
             @Html.Label("UserName:") <input type="text" id="txtImg" name="txtImg" /><br /><br />      
                @Html.Label("Upload:")<input type="file" name="image" id="upload"/><br/>    
            <div id="Preview">
            Preview<img id="blah" src="#" alt="your image" />
        </div>
            <input class="btn btn-success btnUpload" type="submit" value="upload" />
        }
    <table style="border:1">
        @foreach (var item in Model)
        {
            <tr style="border:1">
                <td><img src="@item" alt="image" class="img-responsive" width="100" height="100" /></td>
                <td><button class="btn btn-primary Delete"  id="@item" onclick="deleteImage('@item');">Delete</button></td>
                <td><a class="btn btn-primary Download" href="@item" target="_blank">Download Image</a></td>
                <td><button class="btn btn-primary Download"  onclick="updateImage('@item');">UpdateImage</button></td>
            </tr>
        }

我将斑点直接发送到视图中,这基本上是问题所在。如何使用模型插入文本,bloburl,图像?

如何在 asp.net mvc 中使用 Html.BeginForm() 发送多个参数

访问 在 MVC 4 中上传/显示图像

这对我上传图像有很大帮助

在将数据从视图检索到控制器方面,更好的方法是使用视图模型,例如

public class ExampleViewModel
{
    public string Image {get; set;}
    public string Text {get; set;}
}

您的视图看起来像

@model YourProject.Models.ExampleViewModel
@using (Html.BeginForm("ExampleController","Home",FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.TextBoxFor(model => model.Text)
    <input id="Image" type="file" name="Image" data-val="true" />
}

和控制器

public ActionResult ExampleController(ExampleViewModel model)
{
    //Not sure how you wanted to get the image so I just put this in here
    foreach (string file in Request.Files)
    {
        HttpPostedFileBase hpf=Request.Files[file] as HttpPostedFileBase;
        if(hpf.ContentLengh==0)
        continue;
        string folderPath = Server.MapPath("~/yourFolderPath");
        Directory.CreateDirectory(folderPath);
        string savedFileName = Server.MapPath("~/yourFolderPath/" + hpf.FileName);
        hpf.SaveAs(savedFileName);
        model.Image="~/yourFolderPath/" + hpf.FileName;
    }
    //this variable is getting the text from the ViewModel
    string text=model.Text;
}

希望这对:)有所帮助