[RESOLVED]How to process a div as a form input

hi,

I have this jQuery plugin which only works well with divs, but I need this div to be processed as a form input

<div class="editable form-control input-lg graf--h3" data-type="text" data-name="title" data-placeholder="Title" data-maxlength="60">@sqlSelect.Title</div>
             <div class="editable form-control content" data-type="text" data-name="sub" data-placeholder="Sub-Title (Optional)" data-maxlength="170">@sqlSelect.SubTitle</div>
        <div data-name="content" class="editable controls">@Html.Raw(sqlSelect.Content)</div>

But I do not know how to retrieve the name attribute to be used in my IsPost Query
if(Request["save"]!=null){
 
var saved= "UPDATE Feeds SET Title=@0, SubTitle=@1, Content=@2,IsSave=@3,Publish=@4,UploadDate=@5,AdminPublish=@6 WHERE Link=@7";
db.Execute(saved,title,sub,content,true,false,DateTime.Now,false,storyID);
 
         message = "'"+title+"' has been saved succesfully!!";
                  
         Session["saved"] = message;
         Response.Redirect("~/p/drafts");
 
     }//End of If Save

I keep getting errors because there is no value being retrieved and passed through the name attribute

The content won’t be posted by default because the element in which is sits is not a form field. You will have to use jQuery to reference the element, obtain it’s text or html value and post that:

var content = $('div[data-name="content"]').html();
$.post('your_post_hanlding_page', {content: content}, function(){...

Mike, I’m not too good with manipulating jquery scripts..

Can you kindly finish the code block, that way I understand and know how to implement it.

Thannks

Actually it may be easier to just assign the html from the div to a hidden field via jQuery:

$('input[name="save"]').on('click', function(){
    $('input[name="content"]').val($('div[data-name="content"]').html());
});

This assumes that the form is submitted with an input type=submit with the name of "save". You also need to add a hidden field with the name of "content":

@Html.Hidden("content")

Finally, you need to use Request.Unvalidated to get the value when you process the submitted form:

var content = Request.Unvalidated("content");

Leave a Reply