绑定值在Asp.Net MVC应用2零件模型 [英] Bind value to model in Asp.Net MVC Application Part 2

查看:236
本文介绍了绑定值在Asp.Net MVC应用2零件模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是形式的后续绑定值模型Asp.Net MVC应用程序

我有不同的控制类模型。相关代码:

I have a Model with different control classes. The relevant code:

public class FileUploadModel
    {
        public HttpPostedFileBase File { get; set; }
    }



我有以下相关的代码的局部视图:

I have a partial view with the following relevant code:

@Html.TextBoxFor(x => x.File, new { type = "file", id = "File", name = "File" }) 

再有是其中局部视图呈现与以下相关的主视图代码:

Then there is a main view in which the partial view is rendered with the following relevant code:

@using (Ajax.BeginForm("ActionMethods", "Index", new AjaxOptions { UpdateTargetId = "parameterList" }, new { enctype = "multipart/form-data" }))
{

  <div id="parameterList">
        <div id="verifyBtnDiv" style="display:none;">

             **THIS IS WHERE THE PARTIAL VIEW AS SHOWN ABOVE WOULD BE RENDERED**

            <input type="submit" id="verifyBtn" value="Verify"/>

        </div>
 </div>

}



发生现在,当提交的文件不绑定到模型属性。控制进入控制器,但我调试并看到其空。对此有什么建议?

Now when the submit happens, the file does not binds to the model property. The control passes to the controller but i debug and see that its null. ANy suggestions regarding this?

推荐答案

有与您发布,这将阻止你正试图从做代码的几个问题加工。

There are a few issues with the code you posted that will prevent what you are attempting to do from working.

首先,我相当肯定,你不能使用 @ Html.TextBoxFor 帮手,并将其转换为文件输入。如果现在的工作,因为你是压倒一切的这是什么意思放出来,并在未来有可能打破我不会依赖于它。让我们刚刚推出一个文件输入一个ID和姓名匹配您的视图模型属性。

First, I am pretty certain that you cannot use the @Html.TextBoxFor helper and convert it to a file input. If it works now, I would not rely on it as you are overriding what it is meant to put out and might break in the future. Let's just put out a file input with an Id and Name matching your ViewModel property.

<input type="file" name="File" Id="File/>

接下来,您的不能中的使用 Ajax.BeginForm()来上传文件。这是AJAX的限制,不能与Ajax.BeginForm的问题。因此,我们将需要更新你的表单元素到正常,Html.BeginForm,用适当的是enctype (这很重要)

Next, you cannot use Ajax.BeginForm() to upload files. It is a limitation of AJAX, not an issue with the Ajax.BeginForm. So, we will need to update your form element to a normal, Html.BeginForm, with the proper enctype (this is important)

@using (Html.BeginForm("Upload", "MyControllerName", FormMethod.Post, new { enctype = "multipart/form-data"}))
{
    <div id="parameterList">
        <div id="verifyBtnDiv" style="display:none;">
            <!-- Chose to just put the one line here instead of calling a partial-->
            <input type="file" name="File" Id="File/>
            <input type="submit" id="verifyBtn" value="Verify"/>
        </div>
 </div>
}



最后,如果你有/上传通过AJAX的文件中,有需要的很多对图书馆好的建议用于阿贾克斯文件上传。我个人比较喜欢 jQuery.Form 插件,因为它是在它处理的方式很透明文件上传。

Lastly, if you have to/required to upload the file via AJAX, there are plenty of good recommendations on libraries to use for Ajax file uploads. I personally like the jQuery.Form plugin as it is pretty transparent in the way it handles file uploads.

这篇关于绑定值在Asp.Net MVC应用2零件模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆