Friday, January 11, 2019

Post/Send Anonymous object to MVC/WebAPI Action/Method


I was building an MVC application where we need to give the option to the user to maintain the reference/lookup data through the UI. To active this I created different model and partial views. In order to render the partial view I need to create the different action methods and code wise it will be repetition of code.

So I finalized to create one action method that take 2 parameter, the first parameter will tell me the type and second parameter will be object Type. Let me show me how I achieve this with an example.

Step 1 – Model
namespace WebApplication2.Models.Lookup
{
    public class User
    {
        public int UserID { get; set; }
        public string UserCode { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int IsActive { get; set; }
        public int UserRole { get; set; }
    }
}

Step 2 – Action Method

[HttpPost]
    public JsonResult SaveLookup(string lookupType, object objLookup)
    {

        if (lookupType == "Users")
            User uObject = JsonConvert.DeserializeObject<User>(objLookup);
        else if (lookupType == "xyz")

        return Json("");
    }

Step 3 – Client Side Code

function SaveLookup() {
    debugger;
    var userData = {};
    userData.lookupType = "Users";
    userData.objLookup = JSON.stringify({ "UserID"1"UserCode""XYZ""FirstName""FName""LastName""LNAme""IsActive""1""UserRole""2" });
    $.ajax({
        url: '/home/SaveLookup',
        dataType: 'json',
        type: 'POST',
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(userData),
        success: function (result) {
            debugger;
            $("#partialviews").html(result);
        },
        error: function (xhr) {
            debugger;
            alert(xhr);
        }
    });
}

Step 4 Partial View

@model WebApplication2.Models.Lookup.User
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    
   
       

User

       

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.UserID)

       
            @Html.LabelFor(model => model.UserCode, htmlAttributes: new { @class = "control-label col-md-4" })
           
                @Html.EditorFor(model => model.UserCode, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.UserCode, "", new { @class = "text-danger" })
           

       


       
            @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-4" })
           
                @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
           

       


       
            @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-4" })
           
                @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
           

       


       
            @Html.LabelFor(model => model.IsActive, htmlAttributes: new { @class = "control-label col-md-4" })
           
                @Html.EditorFor(model => model.IsActive, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.IsActive, "", new { @class = "text-danger" })
           

       


       
            @Html.LabelFor(model => model.UserRole, htmlAttributes: new { @class = "control-label col-md-4" })
           
                @Html.EditorFor(model => model.UserRole, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.UserRole, "", new { @class = "text-danger" })
           

       


       
           
                
           

       

   

}

    Back to List



No comments: