回发后@ Html.DisplayFor()值不会改变

时间:2014-02-03 17:53:04

标签: asp.net-mvc razor postback html-helper

发送数据后

@Html.DisplayFor()值不会更改。我读了一篇关于这个问题的文章并且这样说;仅发送EditorForTextBoxForTextAreaFor等数据并更改状态。这是真的吗?如何在回发后更改此值?

查看

@model HRProj.Model.Person

@using(Html.BeginForm("Skills", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })){
    @Html.HiddenFor(m => m.SkillDoc.Filename)
    <span class="file-upload">
        <span>Choose a file</span>
        <input type="file" name="file" />
    </span>
    File name : @Html.DisplayFor(m => m.SkillDoc.Filename)
     <button>Upload</button>

}

控制器

public ActionResult Skills(int? id)
{
    Others oparations...
    var model = new Person { SkillDoc = db.GetSkillDoc().FirstOrDefault(m => m.PersonId == id) };
    return View(model);
}

[HttpPost]
public ActionResult Skills(Person model, HttpPostedFileBase file)
{
    Others oparations...

    if (ModelState.IsValid)
    {
        SkillDoc doc = new SkillDoc();
        doc.Id = model.SkillDoc.Id;
        doc.PersonId = model.SkillDoc.PersonId;
        doc.CvDoc = (file != null) ? file.FileName : model.SkillDoc.CvDoc;
        db.SkillDocCRUD(doc, "I");

        TempData["eState"] = "The record adding successfully";

        if (file != null)
        {
            file.SaveAs(Server.MapPath("~/Files/" + file.FileName));
        }
    }
    return View(model);
}

1 个答案:

答案 0 :(得分:2)

请在if块中添加以下行:

model.SkillDoc=doc;

或者更确切地说是重定向到技能操作:

return RedirectToAction("Skills", new{id= model.PersonId});