在剃刀表格视图,包括文件上传 [英] Including File Upload in Razor Form View

查看:105
本文介绍了在剃刀表格视图,包括文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC 3,VB.NET。我在我的应用程序从用户得到基本信息的表格,然后允许他们上传简历。我现在实现这一权利通过重定向到一个不同的形式,是唯一的目的就是选择要上传的文件,然后提交它..在我看来,马虎。我已经试图从相同的形式做,但该文件是丢失的提交。虽然我不能肯定它,我相信那是因为我不使用形式正确的语法来处理文件......下面是查看到目前为止,然后控制器功能后..

  @ModelType xxxxxxxx.courseproposal
@ code
ViewData的(标题)=课程方案
结束code
<脚本的src =@ Url.Content(〜/脚本/ jquery.validate.min.js)TYPE =文/ JavaScript的> < / SCRIPT>
<脚本的src =@ Url.Content(〜/脚本/ jquery.validate.unobtrusive.min.js)TYPE =文/ JavaScript的>< / SCRIPT>@using Html.BeginForm()
@ Html.ValidationSummary(真)
@<&字段集GT;
 <表>
    &所述; TR>
        <第i presenter 1 LT; /第i
    < / TR>    &所述; TR>
        < TD>首先名称和LT; / TD>
        < TD>姓< / TD>
        < TD>标题< / TD>
        < TD>电话号码< / TD>
        < TD>邮箱地址:LT; / TD>    < / TR>
    &所述; TR>
        <第i @ Html.EditorFor(功能(模型)model.Name_First1)@ Html.ValidationMessageFor(功能(模型)model.Name_First1)LT; /第i
        <第i @ Html.EditorFor(功能(模型)model.Name_Last1)@ Html.ValidationMessageFor(功能(模型)model.Name_Last1)LT; /第i
        <第i @ Html.EditorFor(功能(模型)model.Title_1)@ Html.ValidationMessageFor(功能(模型)model.Title_1)LT; /第i
        <第i @ Html.EditorFor(功能(模型)model.phone_number1)@ Html.ValidationMessageFor(功能(模型)model.phone_number1)LT; /第i
        <第i @ Html.EditorFor(功能(模型)model.email_address1)@ Html.ValidationMessageFor(功能(模型)model.email_address1)LT; /第i
         <第i @ Html.Label(文件,文件名)<输入类型=文件名称=文件ID =文件/> < /第i
    < / TR>
< /表>< D​​IV ID =sidebar3> &所述p为H.;
 <输入类型=提交值=提交课程建议书/>
 &所述; / P> &所述p为H.;
 @ Html.ActionLink(返回目录,索引)
 &所述; / P>
 < / DIV>< /字段集>
使用完

和POST功能是这样的:

 < AcceptVerbs(HttpVerbs.Post)GT;
    功能CourseProposal(BYVAL courseprop作为courseproposal)作为的ActionResult
        courseprop.conf_Number = _AnnualNumber
        db.courseproposals.AddObject(courseprop)
        db.SaveChanges()
        _id = courseprop.idCourseProposal
        昏暗_filename作为字符串=的String.Empty
        对于每一个文件作为字符串在Request.Files
            DIM HPF作为HttpPostedFileBase = TryCast(Request.Files(文件),HttpPostedFileBase)
            如果hpf.ContentLength = 0则
                继续
            万一
            昏暗savedfileName作为字符串= Path.Combine(AppDomain.CurrentDomain.BaseDirectory)+\\ CoursePro presumes \\+ Path.GetFileName(hpf.FileName)
            hpf.SaveAs(savedfileName)
            _filename = hpf.FileName
        下一个


解决方案

您需要的形式 ENCTYPE =的multipart / form-data的

尝试

  Html.BeginForm(NULL,NULL,FormMethod.Post,新{ENCTYPE =的multipart / form-data的})

修改:那当然是C#。在VB中应该读

  Html.BeginForm(没什么,没什么,FormMethod.Post,新增功能{.enctype =的multipart / form-data的})

MVC 3, VB.NET. I have a form in my app that gets basic information from the user and then allows them to upload a resume. I am accomplishing this right now by using a redirect to a different form that's only purpose is to select the file for uploading and then submit it.. Sloppy in my opinion.. I have tried to do it from the same form but the file is being lost on submit.. While I am not certain of it I believe it is because I am not using the correct syntax in the form to handle the file... Below is the View so far and then the controller post function..

@ModelType xxxxxxxx.courseproposal
@Code
ViewData("Title") = "Course Proposal"
End Code
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">         </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@Using Html.BeginForm()
@Html.ValidationSummary(True)
@<fieldset>
 <table>
    <tr>
        <th>Presenter 1</th>
    </tr>

    <tr>
        <td>First Name</td>
        <td>Last Name</td>
        <td>Title</td>
        <td>Phone Number</td>
        <td>Email Address</td>

    </tr>
    <tr>
        <th>@Html.EditorFor(Function(model) model.Name_First1) @Html.ValidationMessageFor(Function(model) model.Name_First1)</th>
        <th>@Html.EditorFor(Function(model) model.Name_Last1) @Html.ValidationMessageFor(Function(model) model.Name_Last1)</th>
        <th>@Html.EditorFor(Function(model) model.Title_1) @Html.ValidationMessageFor(Function(model) model.Title_1)</th>
        <th>@Html.EditorFor(Function(model) model.phone_number1) @Html.ValidationMessageFor(Function(model) model.phone_number1)</th>
        <th>@Html.EditorFor(Function(model) model.email_address1) @Html.ValidationMessageFor(Function(model) model.email_address1)</th>
         <th>@Html.Label("file","Filename:")<input type="file" name="file" id="file" />  </th>
    </tr>
</table> 

<div id="sidebar3">

 <p>
 <input type="submit" value="Submit Course Proposal" />
 </p>

 <p>
 @Html.ActionLink("Back to List", "Index")
 </p>
 </div>

</fieldset>
End Using

And the post Function is like this:

   <AcceptVerbs(HttpVerbs.Post)>
    Function CourseProposal(ByVal courseprop As courseproposal) As ActionResult
        courseprop.conf_Number = _AnnualNumber
        db.courseproposals.AddObject(courseprop)
        db.SaveChanges()
        _id = courseprop.idCourseProposal
        Dim _filename As String = String.Empty
        For Each File As String In Request.Files
            Dim hpf As HttpPostedFileBase = TryCast(Request.Files(File), HttpPostedFileBase)
            If hpf.ContentLength = 0 Then
                Continue For
            End If
            Dim savedfileName As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CoursePropResumes\" + Path.GetFileName(hpf.FileName)
            hpf.SaveAs(savedfileName)
            _filename = hpf.FileName
        Next

解决方案

Your form needs enctype="multipart/form-data".

Try

Html.BeginForm(null, null, FormMethod.Post, new { enctype="multipart/form-data"})

EDIT: That of course is for C#. In VB it should read

Html.BeginForm(Nothing, Nothing, FormMethod.Post, New With { .enctype="multipart/form-data"})

这篇关于在剃刀表格视图,包括文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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