1

Here i am saving the data from form fields and i want to display in my list page. but i am not able to get the list. can any one suggest on this.

here is my list.cshtml code

var saveTrainingProgram = function () {

    console.log('in save');

    $.ajax({
        url: '/Affiliate/TrainingProgram/Save',
        type: 'POST',
        data: {
            trainingProgramName: ff.trainingProgramName.val(),
            colorCode: ff.colorCode.val()
        }
    }).done(function (response) {
        if (response.status === 'success') {



        } else {

        }


    }).fail(function (jqXHR, textStatus) {

        $.isLoading('hide');
    });


};


};

here is my controller code:

[HttpPost]
[Authorize(Roles = "Affiliate")]
public ActionResult Save(string trainingProgramName, string colorCode)
{
    CbJsonResponse response = new CbJsonResponse();

    int errorCount = 0;
    List<string> errorMessages = new List<string>(5);


    if (string.IsNullOrEmpty(trainingProgramName))
    {
        errorCount++;
        errorMessages.Add("Error: The Training Program Name is required.");
    }

    if (string.IsNullOrEmpty(colorCode))
    {
        // This is not a required field, assign a default value.
        colorCode = CbConstants.TrainingProgramDefaultColorCode;
    }


    if (errorCount == 0)
    {
        Entities.TrainingProgram trainingProgram = new Entities.TrainingProgram();
        trainingProgram.Name = trainingProgramName;
        trainingProgram.ColorCode = colorCode;

   if (TrainingProgramRepository.Instance.SaveTrainingProgram(trainingProgram))
        {

            response.Status = "success";

            TrainingProgramListEntry listEntry = new TrainingProgramListEntry()
            {
                ID = trainingProgram.ID,
                Name = trainingProgram.Name,
                ColorCode = trainingProgram.ColorCode
            };

            // Include the list entry in the response.
            response.Data.Add(listEntry);
        }
    }
    else
    {
        response.Status = "failure";
        response.Meta.Add("errorCount", Convert.ToString(errorCount));
        response.Data.Add(errorMessages);
    }

    return Json(response, "application/json", System.Text.Encoding.UTF8, JsonRequestBehavior.AllowGet);
}
Sampath
  • 63,341
  • 64
  • 307
  • 441
Micky
  • 11
  • 5
  • Does your controller return the correct list ? Can you see the right response from your browser console ? – PMerlet Dec 19 '16 at 13:18
  • 1
    Where **exactly** is the error occurring? Is your controller method receiving the values that you are expecting from you ajax function? Or is something happening inside of your controller method that you aren't expecting? Please be more clear so that we can be more efficient in helping. – Grizzly Dec 19 '16 at 13:20

1 Answers1

0

You can easily overcome this issue if you're using ViewModels. Try as shown below and let us know about the result...

public class MyVmModel
{
    public string TrainingProgramName { get; set;}
    public string ColorCode { get; set;}
    public string Status { get; set;}

}


[HttpPost]
[Authorize(Roles = "Affiliate")]
public ActionResult Save(MyVmModel myVm)
{
    //your code here

    myVm.Status= "your message here"
    return Json(myVm);
}

Part of the Ajax :

$.ajax({
        url: '/Affiliate/TrainingProgram/Save',
        type: 'POST',
        data: {
            trainingProgramName: ff.trainingProgramName.val(),
            colorCode: ff.colorCode.val()
        }
        accept: 'application/json' //you have to give this here
})
Sampath
  • 63,341
  • 64
  • 307
  • 441
  • public class TrainingProgramListEntry { [DataMember] public int ID { get; set; } [DataMember] public string Name { get; set; } [DataMember] public string ColorCode { get; set; } [DataMember] public string BackgroundColor { get { return ("background-color: " + ColorCode + ";"); } } public bool Active { get; internal set; } } } – Micky Dec 20 '16 at 03:08
  • above code is viewmodel. can you suggest me how to resolve this issue – Micky Dec 20 '16 at 03:11
  • i have done but there is same error where i cant find the new data row – Micky Dec 20 '16 at 05:43
  • what happened when you debug it on browser's console window ? is that return correct message from the server or what ? – Sampath Dec 20 '16 at 07:12
  • On Save click we are passing the trainingprogram viewmodel in that by default id is setting as 0 . so getting error as cant pass the values to identity column which is set off – Micky Dec 21 '16 at 02:57
  • The problem is on ID. If you set a field as IDENTITY you can't normally assign it a value - the IDENTITY property marks it as allowing the database to automatically assign an incrementing value to the column.You can read more about it here : http://stackoverflow.com/questions/23808967/cannot-insert-explicit-value-for-identity-column-in-table-when-identity-insert – Sampath Dec 21 '16 at 05:40
  • can you have a look at the controller code? there i have used entities in if statement can i use entities directly. i am not the one to modify the database. can i change it 0 to null pragmatically. – Micky Dec 21 '16 at 05:58
  • what happen when you remove this line `ID = trainingProgram.ID,` ? – Sampath Dec 21 '16 at 07:00
  • same error. as i said before. i don't know why it is happening. – Micky Dec 21 '16 at 07:04
  • then you can do this `get the highest value in the field, add one to it and then assign that value`.But this is not good at all.you need to discus about this issue with your DBA. – Sampath Dec 21 '16 at 07:49