TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Pravin Ghadge
564
2.1k
587.4k
MVC-Pass Model data from View to Controller using Ajax
Dec 6 2017 12:54 AM
Hi All,
I want to pass the model from view to controller using Ajax
But I am getting null paramter value. Iam not able to understand where iam wrong.
//Model Class
public
class
CNModel
{
public
CNModel()
{
}
public
string
ID {
get
;
set
; }
public
string
Name {
get
;
set
; }
}
//csHTML
@model CN_MVC.Models.CNModel
@
using
Newtonsoft.Json
@
using
(Html.BeginForm(
new
{ @id =
"CNform"
}))
{
<label>ID</label>
@Html.TextBoxFor(m => m.ID,
new
{ @
class
=
"form-control"
, id =
"txtID"
, placeholder =
"Enter ID"
})
<label>Name</label>
@Html.TextBoxFor(m => m.Name
new
{ @
class
=
"form-control"
, id =
"txtName"
, placeholder =
"Enter Name"
})
<input type=
"button"
id=
"btnShow"
class
=
"btn"
value=
"Pass To Controller"
onclick=
"Show()"
/>
}
<script type=
"text/javascript"
>
function Show() {
debugger;
//var data = @Html.Raw(JsonConvert.SerializeObject(Model));
var data =
'@Html.Raw(Json.Encode(Model))'
;
$.ajax({
type:
'POST'
,
// dataType: 'html', // this can be omitted - the ajax() function will work it out
cache:
false
,
url:
'/CN/Show'
,
data: data,
success: function (data, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
}
</script>
//Controller
[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
public
string
Show(
string
modelParameter)
{
var DeserializedModel = JsonConvert.DeserializeObject<RCNModel>(modelParameter);
}
Reply
Answers (
3
)
unable to get some client computer name visiting my intranet
DiskInformation of another remote computer