This is a migrated thread and some comments may be shown as answers.

[Solved] Combo/Autocomplete Retains Values Even When Model and ViewData are Cleared

1 Answer 117 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Sydney
Top achievements
Rank 1
Sydney asked on 15 Jun 2011, 11:59 PM
I have two telerik controls on a form (one is ComboBox, one is Autocomplete).  The form is for Adding a new student object.  There are two buttons:  Save, Save and New.   Save and New posts to the Controller's save method, but instead of redirecting back to a list of existing students, it is meant to return the Add Student screen.  I have the following code in the Controller action:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddStudent(StudentProfileDto student, int addAnother)
{
    var teacher = SessionWrapper.GetTeacher(HttpContext);
    var model = new EditStudentModel
    {
        Teacher = teacher
    };
 
    if (ModelState.IsValid)
    {
        AddStudentToTeacher(student);
 
        if (addAnother == 0)
            return RedirectToAction("StudentRoster");
 
        ViewData = null;
        model.Student = new StudentProfileDto();
    }
    else
    {
        model.Student = student;
    }
 
    return View(model);
}


All non-Telerik controls are cleared as expected.  However, the Telerik controls retain the values from the Student that was just added.

Here's one of the Telerik control's code:

@{
  Html.Telerik()
      .AutoCompleteFor(model => model.Student.TagString)
      .Encode(false)
      .BindTo(Model.Teacher.Tags)
    //.AutoFill(true)
      .HtmlAttributes(new { @class = "form-input" })
      .HighlightFirstMatch(true)
      .Filterable(filtering =>
      {
        filtering.FilterMode(AutoCompleteFilterMode.StartsWith);
      })
      .Multiple(multi =>
      {
        multi.Separator(",")
            .Enabled(true);
 
      })
      .Render();
}

1 Answer, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 17 Jun 2011, 12:41 PM
Hello Sydney,

 
In order to persist custom value sent to the server using ComboBox, we use ValueProvider of the controller. As you probably noticed the combobox UI component has two inputs, one for SelectListItem.Text and second for the SelectListItem.Value. ASP.NET MVC will persist in the ModelState only the SelectListItem.Value which corresponds to the ComboBox.Name. The SelectListItem.Text is persisted only in the FormCollection.
Hence if you need to clear ComboBox UI component I will suggest you call RedirectToAction or to clear combobox on the client-side. 

Regards,
Georgi Krustev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
ComboBox
Asked by
Sydney
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Share this question
or