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 8365903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:46:38+00:00 2026-06-09T12:46:38+00:00

I have this ViewModel to represent each Category and it’s several Sub-Categories in my

  • 0

I have this ViewModel to represent each Category and it’s several Sub-Categories in my project:

public class Categories
{
    //a simple string for the category name
    [Required]
    public string Cat_Name { get; set; }

    //a list of strings for the sub-categories
    public List<string> SubCat_Name { get; set; }
}

I pass this model to the view, and I use it to construct my form like this:

@using (Html.BeginForm("Category", "Admin", FormMethod.Post))
{
    @Html.TextBoxFor(m => m.Name, new { @class = "Cat" })

    @Html.TextBoxFor(m => m.SubName, new { @class = "Sub" })

    @Html.TextBoxFor(m => m.SubName, new { @class = "Sub" })

    @Html.TextBoxFor(m => m.SubName, new { @class = "Sub" })

    @Html.TextBoxFor(m => m.SubName, new { @class = "Sub" })

    <input type="button" value="Create New Category" name="Category" onclick="DoIt()" />
}

And as you see, the javascript function “DoIt()” is called by the click on the input button.
here’s the function :

function DoIt() {
    var stringArray = new Array();
    //I put all of the sub-categories inside an array, to be like a list<string>

    $(".Sub").each(function (index, value) {
        stringArray[index] = this.value;
    });

    $.ajax({
        url: '/Admin/Category',
        type: "POST",
        data: JSON.stringify($(".Cat").attr("value") , stringArray),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function () { console.log("post done"); },
        error: function () { }
    });
}

As you can see, in the ajax method I’m trying to pass the “Category Name” $(".Cat").attr("value") and it’s “Sub-Category Names” stringArray to the corresponding action method which requires Categories datatype I created as a viewmodel before. It looks like this:

public ActionResult Category(Categories CAT)
{
    //Do something
    return View();
}

The problem is the ajax method can’t post two datatypes at once, or just that I don’t know how to do it.

I also tried this :

var postdata = {
    Name: $(".Cat").attr("value"),
    SubName: stringArray
};

and put postdata inside the JSON.stringify() instead. But still nothing is passed to the action method.

  • 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-06-09T12:46:40+00:00Added an answer on June 9, 2026 at 12:46 pm

    I think you are very close.

    Try this

    var postdata = {
        Cat_Name: $(".Cat").val(),
        SubCat_Name: stringArray
    };
    

    &

    $.ajax({
            url: '/Admin/Category',
            type: "POST",
            data: JSON.stringify(CAT:postdata),
            dataType: "json",
            contentType: "application/json",
            success: function () { console.log("post done"); },
            error: function () { }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this ViewModel public class CustomerSuscribeViewModel : IValidatableObject { [DataMember(IsRequired = true)] [DataType(DataType.Text)]
I have a class that roughly looks like this: public class ViewModel { public
I have a ViewModel defined like this: public class LocationTreeViewModel<TTree> : ObservableCollection<TTree>, INotifyPropertyChanged TTree
My situation is this: I have this ViewModel: public class RealtyViewModel { public RealtyViewModel()
I have this line in my View: @Html.EditorForModel() And this is my ViewModel: public
I have this working JS: $(function() { // Class to represent a row in
I have just started with knockout this Model and viewmodel: $(function() { // Class
I have this block of xaml and I made a ViewModel which contains a
I have a viewmodel which is subscribing to the event NavigationCompletedEvent. This viewmodel need
I have a View / ViewModel where a ProductList is loaded. This list is

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.