将记录ID传递到视图中,然后返回Form Post

时间:2011-12-09 03:37:01

标签: asp.net-mvc vb.net asp.net-mvc-3

MVC 3. VB.NET应用程序..我试图传递一个id来跟踪哪个记录需要保存文件名..问题是id被传递到视图但是在回到控制器后,它正在丢失......我现在已经敲了好几个小时了,而且我被卡住......任何想法?

 @ModelType xxxxxxxxxxx.fileUploadVM
 @Code
 ViewData("Title") = "Upload Resume"
 Layout = "~/Views/Shared/_Layout.vbhtml"
 End Code
 @Html.HiddenFor(Function(m) m.id)
<h2>Upload Resume</h2>
<p style="text-align: center">Please Upload your resume below.</p>
<ul>

<form action="UploadResume" method="post" enctype="multipart/form-data">   
<label for="file">Filename:</label>    <input type="file" name="file" id="file" />  
<input type="submit" name="submit" value="Submit" /></form> 
</ul> 

控制器功能如下所示:

 Function UploadResume(ByVal id As Integer) As ActionResult
    Dim filevm As New fileUploadVM
    filevm.id = id
    Return View(filevm)
End Function

<AcceptVerbs(HttpVerbs.Post)>
Public Function UploadResume(ByVal fvm As fileUploadVM) As ActionResult

    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

    Dim e As courseproposal = db.courseproposals.Single(Function(f) f.idCourseProposal = fvm.id)
    e.resume_file_Name = _filename
    UpdateModel(e)
    db.SaveChanges()
    Return RedirectToAction("Index")

End Function

1 个答案:

答案 0 :(得分:1)

您的@ Html.HiddenFor需要放在您的表单中。