0

我有这样的看法:

<div id="basic-information-container">
    @Html.Action("MyBasicInformation")
</div>

<div id="cv-container">
    @Html.Action("MyCv")
</div>

<div id="experiance-container">
    @Html.Action("MyExperiances")
</div>

<div id="academic-background-container">
    @Html.Action("MyAcademicBackgrounds")
</div>

部分视图 MyCv 是:

@model Model.Profile.CvModel

<script type="text/javascript">
    $(function () {
        $("#cv-container form").submit(function () {
            $(this).ajaxSubmit({
                target: "#cv-container",
                cache: false
            });

            return false;
        });
    });
</script>

@using(Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Cv</legend>
        @Html.EditorFor(model => model.File) 
        @* I have working editor template for HttpPostedFileBase, nothing to worry about this*@
        @Html.ValidationMessageFor(model => model.File)
        <p>
            <input type="submit" value="Upload" />
        </p>
    </fieldset>
}

这是 CvModel 的代码

public class CvModel {
    public int? Id { get; set; }

    [Required]
    public HttpPostedFileBase File { get; set; }

    public CvModel() {

    }

    public CvModel(int? cvId) {
        Id = cvId;
    }
}

这是 MyCv 的 post 方法

[HttpPost]
public ActionResult MyCv(CvModel model) {
    // upload cv to database
    return PartialView(model);
}

现在,问题是,当我出于某种未知原因上传 CV 时,部分视图MyBasicInformation, MyExperiances and MyAcademicBackgrounds正在重新加载。我找不到我做错了什么。我在哪里做错了?

顺便说一句,我已经确认MyBasicInformation, MyExperiances and MyAcademicBackgrounds没有调用 get 操作。视图正在直接重新加载。

4

1 回答 1

2

由于您使用的是子操作,因此您必须在表单中明确指定要发布到的操作,否则表单可能无法提交到正确的操作。此外,您的表单上缺少enctype="multipart/form-data"上传文件所必需的一个:

所以:

@using(Html.BeginForm()) {

应该变成:

@using (Html.BeginForm("MyCv", "SomeController", FormMethod.Post, new { enctype = "multipart/form-data" }))

Also check with FireBug for potential errors in the console. Make sure the AJAX request is sent successfully. Ensure that the .submit() event is reattached the second time.

于 2012-07-11T06:05:19.183 回答