Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4084450
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:26:28+00:00 2026-05-20T18:26:28+00:00

I have a strongly typed Category View, showing all Category (in a grid) …

  • 0

I have a strongly typed Category View, showing all Category (in a grid) …
Under that grid I have the fields to Add/Edit a new Category details: several textbox and radiobox, like :
– Category Name
– Category Description
– Category Imagem
etc

So, When the user select a Category on Grid, it fills the fields via JQuery Ajax…

  $.ajax({
        url: '/Category/Get',
        type: 'POST',
        dataType: 'json',
        data: {
            idCategory: ...
        }
    })
    .success(function(category,success) {                
        // Update TextBox fields
        $(".ajax").each(function(){
            $(this).val(category[$(this).attr("id")]);
        });
        //Other fields ...             
     })      

My Controller

[HttpPost]
public ActionResult Get(string idCategory)
{
     if (ModelState.IsValid)
     {
         var _category = _categoryRepository.Get(Convert.ToInt16(idCategory));

         if (Request.IsAjaxRequest())
             return Json(_category);
         return RedirectToAction("Index");
     }
     if (Request.IsAjaxRequest())
         return Json(new Category());
     return View();
 }

It´s working fine !

But I´d like to know if it is the best way to do that… It is possible to do it another way? Maybe using Ajax.BeginForm?

Thanks

Paul

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:26:29+00:00Added an answer on May 20, 2026 at 6:26 pm

    Two possible ways to do this in my view.

    1) Return a partial view with the category fields in it each time you change the selected category in the grid. Don’t bother with JSON.

    2) If you want to use JSON (which is how I’d probably do it), then use a framework like Knockout.JS to handle the binding. It would look something like this:

    <div id="current">
        Name: <input type="text" data-bind="value: Name"/><br/>
        Description: <input type="text" data-bind="value: Description"/><br/>
        <input type="button" value="Save" data-bind="click: Save" />
    </div>
    
    // javascript viewmodel w/ observable properties (except Id)
    var Category = function(original) {
    this.Id = original.Id;
    this.Name = ko.observable(original.Name);
    this.Description = ko.observable(original.Description);
    
    this.asJson = function() {
        return {
            Id: this.Id,
            Name: this.Name(), // call observables as method to get underlying value
            Description: this.Description()
        };
    };
    
    this.Save = function() {
        $.post("/Category/Update", $.toJSON(this.asJson()));
    };
    }
    
    $.post('/Category/Get', { idCategory: ... }, function(response) {
        var viewModel = new Category(response);
        ko.applyBindings(viewModel, $("#current"));
    }, "json");
    

    Your action could probably be simplified to

    [HttpPost]
    public ActionResult Get(int idCategory)
    {
        var category = _categoryRepository.Get(idCategory) ?? new Category();
        return Json(category)
    }
    

    Then to save just do whatever you normally do on the server

    [HttpPost]
    public ActionResult Update(Category updated)
    {
        var category = _categoryRepository.Get(updated.Id);
        // todo: update category with values from updated model
    }
    

    Edit

    If you don’t want to call applyBindings multiple times, you can use templates instead. (I didn’t test this code but it should be very close.)

    <div id="current" data-bind="template: {name: 'categoryTemplate', data: Current}"/>
    <input type="button" value="Save" data-bind="click: Save" />
    
    <script type="text/html" id="categoryTemplate">
        Name: <input type="text" data-bind="value: Name"/><br/>
        Description: <input type="text" data-bind="value: Description"/>
    </script>
    
    // javascript viewmodel w/ observable properties (except Id)
    var Category = function(original) {
        this.Id = original.Id;
        this.Name = ko.observable(original.Name);
        this.Description = ko.observable(original.Description);
    
        this.asJson = function() {
            return {
                Id: this.Id,
                Name: this.Name(), // call observables as method to get underlying value
                Description: this.Description()
            };
        };
    }
    
    var viewModel = 
    {
        Current = ko.observable({}),
        Save = function() {
            $.ajax({
                    url: "/Category/Update", 
                    data: $.toJSON(this.Current().asJson()),
                    type: "POST",
                    contentType: "application/json; charset=utf-8"
                });
        }
    };
    
    $.post('/Category/Get', { idCategory: ... }, function(response) {
        viewModel.Current(new Category(response));
    }, "json");
    
    $(function(){
        ko.applyBindings(viewModel, $("#current"));
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a strongly typed View class that all my UserControls derive from. It
i have an strongly typed object that represents all of the textbox properties of
I have a strongly typed partial view CheckBox.ascx that renders a checkbox. This is
I have a strongly typed Person view, that I want to render a partial
I have a strongly-typed Partial View that takes a ProductImage and when it is
I have a strongly typed view, Edit, with a model named OrderModel. In this
I have a strongly typed view that is using a viewmodel I created. I
I have a strongly typed partial view that should show the name of an
i have a strongly typed asp.net-mvc view and the Model has a . Links
I'm wanting to have a strongly typed user control that accepts the class PaginatedList<T>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.