1

In my ASP.NET framework MVC project I must send below two data (first data is a list of string and second data is a GUID) with ajax to the action in controller:

0: "{Key:'052c5941-233a-4bd2-80e1-7cfffa34ca44',Value:'Salary1'}"
1: "{Key:'00000000-0000-0000-0000-000000000004',Value:'Salary2'}"
2: "{Key:'00000000-0000-0000-0000-000000000005',Value:'Salary3'}"
3: "{Key:'00000000-0000-0000-0000-000000000003',Value:'Salary4'}"
4: "{Key:'00000000-0000-0000-0000-000000000001',Value:'Salary5'}"
5: "{Key:'00000000-0000-0000-0000-000000000002',Value:'Salary6'}"

and

"6a580cf1-2f05-4621-8a67-8fe0bdd559c2"

My action is as below

 public JsonResult DigestFile(List<string> value1, string pointId)
  {
   ....
  }

How can I do this? Any help will be appriciated!

1
  • 1
    if api change is allowed, I would create specific class that would encapsulate all required properties. class with 2 properties: one of type Dictionary<Guid, string> and another of type Guid Commented Dec 14, 2020 at 7:47

1 Answer 1

1

You can use HTTP post as the following code to do it, you just creat a class with Key and Value, and use it in Controller with a String variable, on a HTTP post Function.

I am not expert in C# I did translate online from my VB code that work well.

In your Views (.cshtml) :

<input id="Button1" type="button" value="button" onclick="SaveMe()" />
<script>
    function SaveMe() {
        var GUID = '6a580cf1-2f05-4621-8a67-8fe0bdd559c2';

        // Creat Object
        var lineItems = new Object();
        lineItems.Entrys = new Array();

        // Example Filling your object, but of course you can get data from another place ...
        lineItems.Entrys[0] = new Object({ Key: '052c5941-233a-4bd2-80e1-7cfffa34ca44', Value: 'Salary1' });
        lineItems.Entrys[1] = new Object({ Key: '00000000-0000-0000-0000-000000000004', Value: 'Salary2' });
        lineItems.Entrys[2] = new Object({ Key: '00000000-0000-0000-0000-000000000005', Value: 'Salary3' });


        $.ajax({
            type: "POST",
            url: "/Home/AjaxMethodDigestFile",
            data: JSON.stringify({ Entrys: lineItems.Entrys, GUID: GUID }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) { alert(response.message); },
            failure: function (response) { alert("failure"); },
            error: function (response) { alert("error"); }
        });
    }
</script>

In your Models:

namespace MyModel1.ViewModel
{
    public class MyClass1
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }
}

In your Controllers:

[HttpPost]
public JsonResult  AjaxMethodDigestFile(ICollection<MyModel1.ViewModel.MyClass1> Entrys, string GUID)
{
    string message = "";
    int counter = 0;

    foreach (var entry in Entrys)
    {
        // How to use this data example
        string Key = entry.Key;
        string Value = entry.Value;
        counter += 1;
        message += Value + ": " + Key + Constants.vbCrLf;
    }

    // The following lines are not necessary, it's just an example code to know what happen and return it to client side ...
    if (counter > 0)
        message = counter.ToString() + " Entrys received" + Constants.vbCrLf + message;
    else
        message = "no entrys";


    var response = new { counter, message };
    
    return Json(response);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that was great and you helped me to solve my problem. But I made some changes in the Action, I mean that the input of "Entrys" was a list of null and I do not know and I did not use model "MyClass1" and the input of my Action is as : public ActionResult DigestFile(List<string> Mappings, string pointId){...}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.