3

有一个问题。为了在我的项目中添加新的空缺,我使用 K-MVC

public ActionResult AddVacancy()
{
    var viewModel=new VacancyViewModel();
    viewModel.AllTechnologies = (from t in _serviceClient.GetTechnologies() select t.Name).ToList();
    return View(viewModel);
}

[HttpPost]
public ActionResult AddVacancy(VacancyViewModel viewModel)
{
     _serviceClient.AddVacancy(viewModel);
     return Json(viewModel);
} 

//------------------------------------------------ --------------------------------------

@using PerpetuumSoft.Knockout
@model Core.ViewDataModel.VacancyViewModel
@{
    var ko = Html.CreateKnockoutContext();
    ViewBag.Title = "New vacancy";
}
...............................
@using (ko.Html.Form("AddVacancy", "Vacancy", ko.Model))
{
...............................................        
<p>
            <input class="submitButton" type="submit" value="Create" />
        </p>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@ko.Apply(Model)

一切正常!感谢 K-MVC!但是,如果我想在成功提交后重定向到另一个操作:

[HttpPost]
        public ActionResult AddVacancy(VacancyViewModel viewModel)
        {
             _serviceClient.AddVacancy(viewModel);
             return RedirectToAction("Index"); 
        } 

我会收到错误,因为以前的表单没有收到 Ajax.success。我该如何处理这个问题?提前致谢!

4

1 回答 1

0

我仍然没有找到解决方案,但无需重定向即可使界面为用户提供更多信息

    @using (ko.If(m => m.SavedSuccessfully))
{
    @ko.Html.Span("NEW VACANCY SAVED TO DATABASE.")
}

@using (ko.If(m => m.SavedSuccessfully == false))
{
     using (ko.Html.Form("AddVacancy", "Vacancy", ko.Model))
     {
         <p>@Html.Label("Name: ")            @ko.Html.TextBox(m => m.Name).ValueUpdate(KnockoutValueUpdateKind.AfterKeyDown) </p>
         <p>@Html.Label("Active : ")         @ko.Html.CheckBox(m => m.IsOpen).ValueUpdate(KnockoutValueUpdateKind.Change) </p>
         <p>@Html.Label("English: ")         @ko.Html.DropDownList(m => m.AllLanguageLevels).Value(m => m.EnglishLevel) </p>
         <p>@Html.Label("German: ")          @ko.Html.DropDownList(m => m.AllLanguageLevels).Value(m => m.GermanLevel) </p>
         <p>@Html.Label("Technologies: ")    @ko.Html.ListBox(m => m.AllTechnologies).SelectedOptions(m => m.NeededTechnology) </p>
         <p>
             <input class="submitButton" type="submit" value="Submit" />
         </p>
     }
}

和控制器:

        [HttpPost]
    public ActionResult AddVacancy(VacancyViewModel viewModel)
    {
        if(ModelState.IsValid)
        {
            _serviceClient.AddVacancy(viewModel);
            viewModel.SavedSuccessfully = true;
        }
        return Json(viewModel);
    }

现在,如果成功保存空缺 - 表格隐藏和信息“新空缺已保存到数据库”。出现。

于 2012-08-31T06:35:42.990 回答