简体   繁体   中英

Redirecting with K-MVC

Have a problem. For add new vacancy in my project I using 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)

All works fine! Thanks K-MVC! But, if I want to redirect to another action after successful submition :

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

I'll receive error, because previous form don't receive Ajax.success. How can I manage this problem? Thanks in advance!

I still don't find a solution but make interface more informative to user without redirection

    @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>
     }
}

and controller:

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

And now if vacancy saved successfully - form hides and information "NEW VACANCY SAVED TO DATABASE." appears.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM