Tuesday, 15 July 2014

How to Pass Model Value to Partial View on Button Click in Asp.net MVC Using C#

7/15/2014 - By Pranav Singh 4

This article will show you how you can pass model value to partial view on button  click in you asp.net mvc application. This article you can use in your MVC3, MVC4, and MVC5 application.

So for this article first we will create a new asp.net mvc application and add a model class file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PartialViewInMvc.Models
{
    public class ParticlaViewmodel
    {
        public string PartialViewMessage { get; set; }
    }
}

Now we will create a new controller file and add create view file. After creating view file we will create partial view. For creating partial view we will add we will right click on controller folder and right click- > Add View. Please check the below screen shot.


Now we will create post method. After doing all the process your code will look as shown below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PartialViewInMvc.Models;

namespace PartialViewInMvc.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            ParticlaViewmodel objparticlaviewmodel = new ParticlaViewmodel();
            return View(objparticlaviewmodel);
        }
        [HttpPost]
        public ActionResult Index(int id=0)
        {
            ParticlaViewmodel objparticlaviewmodel = new ParticlaViewmodel();
            objparticlaviewmodel.PartialViewMessage = "This Value is passed form Model. Here are some country name India, Pakistan, Sri Lanka.";
            return View(objparticlaviewmodel);
        }
    }
}

Now open your partial view and add the below code.

Partial view :
@model  PartialViewInMvc.Models.ParticlaViewmodel
@{
    
    <div>
        <h2>@Model.PartialView1Message</h2>
    </div>
}

Now add the below code in your main view where your want to display the partialview on button click.

@model  PartialViewInMvc.Models.ParticlaViewmodel
@{
    ViewBag.Title = "Index";
}
@using (Html.BeginForm("Index", "Home"))
{

    <div>Load Partial View :<input type="submit" value="Load View" /></div>
    
  
        <div style="border: 1px solid;">
            @Html.Partial("PartialView", new PartialViewInMvc.Models.ParticlaViewmodel { PartialViewMessage = Model.PartialViewMessage })
        </div>
      
}


Now run the application, and select the parameter and click on Load view. 


Now click on button your partial view will load.

                                     
DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

4 comments:

  1. this code is not working :(

    ReplyDelete
  2. how to display data from database using partial view?

    ReplyDelete
    Replies
    1. Hi You just needed to assign the value to model and rest you can refer from the above article,

      Delete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top