Caffeine addicted
Caffeine addicted

Reputation: 324

Render html into View MVC

Hi i am making my project in MVC, i which i want to add the control from the drop-down selection on right side div, (the black rectangle part in the image).

Now i have two requirements

  1. Get the inner html or .html() of the right side div, and save this in the database along with the left side form values
  2. I have another view details where i want to display the values of left side fields and render the inner html on right side, (i have the container div in my details view )..

Create page image

i am getting the html with following button click function

$('#SaveCone').click(function () {
        var a = $('#NewData').html();
        console.log(a);

    });

tell me please if you want another details, thanks for your kind help

Upvotes: 1

Views: 830

Answers (1)

Jot Dhaliwal
Jot Dhaliwal

Reputation: 1500

this is nice question

If you want to store your html into the database, simply use ajax ,

$('#urbutton).click(function () {
        var a = $('#yourForm).html();
        $.ajax({
            type: 'POST',
            url: '/UrController/Uraction',
            data:
              $('#MyForm').serialize() + "&par2=" + a,
            success: function (data) {
                // ` alert(data);
            }
        });
    });

your controller looks like this

    public ActionResult Create(Table table, string par2)
            {
using (urcontext cntxt = new urcontext ())
            {

                if (ModelState.IsValid)
                {

                    table.data = par2;
                    cntxt.SaveChanges();
}... so on

this will store your data into database

How to render the Html from database to the view

simply use

<div id="container">

@HTML.Raw(model.data)

< /div>

Hope this will help you man

Upvotes: 1

Related Questions