[英]Need Javascript or jquery code to pass data to controller
Everytime <a>
tag is pressed i need to submit form. 每次按下
<a>
标记,我都需要提交表单。 I need to keep track the number of times <a>
tag is pressed. 我需要跟踪
<a>
标签被按下的次数。 It starts with 3 and every time its is submitted we increment by 6. I need to pass this number to GetArticleData Controller using Action Method. 它以3开头,并且每次提交时我们都增加6。我需要使用操作方法将此数字传递给GetArticleData Controller。
@{
Layout = "~/Views/Shared/_MasterLayout.cshtml";
AjaxOptions ajaxOpts = new AjaxOptions
{
UpdateTargetId = "tableBody",
Url = Url.Action("GetArticleData")
};
}
<div class="headlines-show-more">
@using(Ajax.BeginForm(ajaxOpts))
{
<a class="button-add-content" href="#">Show More News</a>
}
</div>
<div id="tableBody">
@Html.Action("GetArticleData");
</div>
Controller 控制者
public PartialViewResult GetArticleData()
{
HomePageModel model = new HomePageModel();
model.GetDetails();
return PartialView(model);
}
Partial View 部分视图
@model Models.HomePageModel
@foreach(var item in Model.HeadlinesNews.Skip(0).Reverse().Take(9))
{
<a href="@Url.Action("Index", "Article", new { id = item.PKNewsId, title = item.NewsTitle.Replace(' ', '-') })">
<span>
<span>@item.Category</span>
<span></span>
<img src="@Url.Content("~/Uploads/Images/" + item.Image)">
<span>
<span></span>
<p>@item.NewsTitle</p>
</span>
</span>
</a>
}
You GetArticleData()
does not take any parameters indicating the skip
and take
values, and no where on the client do you store the values. 您
GetArticleData()
不会使用任何参数来指示“ skip
和“ take
值,并且在客户端上的任何位置都不会存储值。 Its not even clear why the model in the partial is HomePageModel
when all you want is a collection of News
items. 甚至不清楚,当您想要的只是一个
News
项目的集合时,为什么部分模型是HomePageModel
。 And there is no reason for a form (its a GET method). 而且没有理由使用表格(它是GET方法)。
The basic approach is 基本方法是
public PartialViewResult GetArticleData(int skipCount, int takeCount)
{
// return a collection of news items based on skip and take, for example
var model = (from news in db.News orderby news.ID select news).Skip(skipCount).Take(takeCount);
if (model.Any())
{
return PartialView(model);
}
else
{
return null; // let the client know there is no point calling this again
}
}
Partial view 部分视图
@model IEnumerable<News>
@foreach(var item in Model)
{
// Generate html for a news item
}
Main View 主视图
<button id="more">Show more news</button>
<div id="news">
// Initially display the first 3 items
@Html.Action("GetArticleData", new { skipCount = 0, takeCount = 3 })
</div>
<script>
var skip = 3; // start at the 4th record
var take = 6; // return 6 records at a time
var hasMoreRecords = true;
var url = '@Url.Action("GetArticleData")';
$('#more').click(function() {
if (!hasMoreRecords) {
return;
}
$.get(url, { skipCount: skip, takeCount: take }, function(response) {
if (!response) {
hasMoreRecords = false; // signal no more records to display
} else {
$("#news").append(response); // append the returned html
skip += take; // update for next iteration
}
});
});
</script>
Make a json object like this. 制作一个像这样的json对象。
$("#submit").click(function () {
var ContactID = $("#txtContactId").val();
var Name = $("#Name").val();
var Gender = $("#Gender").val();
var DOB = $("#DOB").val();
var Photo = $("#Phto").val();
var PersonTypeID = $("#PersonTypeID").val();
var Address1 = $("#txtAddress1").val();
//var Address2 = $("#txtAddress2").val();
var City = $("#txtCity").val();
var State = $("#txtState").val();
var PostalCode = $("#txtPostalCode").val();
var VatNo = $("#txtVatNo").val();
var RegNo = $("#txtRegNo").val();
var Phone = $("#txtPhone").val();
var Email = $("#txtEmail").val();
var AdrKey = $("#AdrID").val();
$.ajax({
url: "Person/Create",
data: {
//'ContactID': ContactID,
'Company': Company,
'Status': Status,
'IsActive': IsActive,
'Comments': Comments,
'Country': Country,
'Address1': Address1,
//'Address2': Address2,
'City': City,
'State': State,
'PostalCode': PostalCode,
'VatNo': VatNo,
'RegNo': RegNo,
'Phone': Phone,
'Email': Email
},
dataType: "json",
type: 'POST',
success: function (data) {
alert("Successfully Inserted!");
},
error: function () {
alert("error");
}
});
});
Post the json function back to controller on submit. 在提交时将json函数发布回控制器。 Hope it helps.
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.