Javascript 使用@Ajax.ActionLink MVC将TextBoxFor值传递给控制器
我已经回答了很多关于“同一”问题的问题,但是没有一个对我有效(或者我完全没有抓住要点) 正如主题所说,我试图使用Ajax.ActionLink将TextBoxFor的值传递给我的控制器,但没有任何效果。我尝试了javascript/jquery,并尝试使用我的模型,所有结果都返回null: 视图: 控制器:Javascript 使用@Ajax.ActionLink MVC将TextBoxFor值传递给控制器,javascript,jquery,ajax,asp.net-mvc,Javascript,Jquery,Ajax,Asp.net Mvc,我已经回答了很多关于“同一”问题的问题,但是没有一个对我有效(或者我完全没有抓住要点) 正如主题所说,我试图使用Ajax.ActionLink将TextBoxFor的值传递给我的控制器,但没有任何效果。我尝试了javascript/jquery,并尝试使用我的模型,所有结果都返回null: 视图: 控制器: public ActionResult Duplicate(int id, string type, string dupName) { var model = _m
public ActionResult Duplicate(int id, string type, string dupName)
{
var model = _masterRepository.FindMasterBudgetById(id);
if(dupName == null || dupName.Trim() == "")
{
dupName = "Duplicated";
}
if (type == "Master")
{
var duplicate = Duplication(model, dupName);
var masterbudgetId = _masterRepository.Save(duplicate);
var budgets = _masterRepository.GetMasterBudgets
.Where(x => x.ParentMasterBudgetId == id && x.Type == "Budget")
.ToList();
//Budgets
foreach (var item in budgets)
{
var linkedAccounts = _budgetAccountRepository.GetBudgetAccounts
.Where(x => x.BudgetId == item.BudgetId)
.ToList();
var duplicateBudgets = Duplication(item, dupName);
duplicateBudgets.ParentMasterBudgetId = masterbudgetId;
var getId = _masterRepository.Save(duplicateBudgets);
foreach (var account in linkedAccounts)
{
var budgetaccount = new BudgetAccount
{
BudgetId = getId,
BudgetAccountId = 0,
AccountId = account.AccountId,
};
var newaccountId = _budgetAccountRepository.Save(budgetaccount);
}
var linkedBudgetEntries = _budgetEntryRepository.GetBudgetLineEntries(item.BudgetId);
foreach(var lineEntryItem in linkedBudgetEntries)
{
var lineEntries = new Notes_Line_Entries
{
EntryLineId = lineEntryItem.EntryLineId,
fkiAccountId = lineEntryItem.fkiAccountId,
fkiNotesColumnId = lineEntryItem.fkiNotesColumnId,
Value = lineEntryItem.Value,
isNewEntry = lineEntryItem.isNewEntry,
budgetId = getId
};
_budgetEntryRepository.SaveDuplicateLineEntries(lineEntries,item.BudgetId);
}
var subbudgets = _masterRepository.GetMasterBudgets
.Where(x => x.ParentBudgetId == item.BudgetId)
.ToList();
//Sub-Budgets
foreach (var subItem in subbudgets)
{
var subBudgetLinkedAccounts = _budgetAccountRepository.GetBudgetAccounts
.Where(x => x.BudgetId == subItem.BudgetId)
.ToList();
var duplicateSubBudgets = Duplication(subItem, dupName);
duplicateSubBudgets.ParentMasterBudgetId = masterbudgetId;
duplicateSubBudgets.ParentBudgetId = getId;
duplicateSubBudgets.BudgetTypeName = subItem.BudgetTypeName;
duplicateSubBudgets.OrganisationId = subItem.OrganisationId;
var getSubBudgetId = _masterRepository.Save(duplicateSubBudgets);
foreach (var account in subBudgetLinkedAccounts)
{
var budgetaccount = new BudgetAccount
{
BudgetId = getSubBudgetId,
BudgetAccountId = 0,
AccountId = account.AccountId,
};
var newaccountId = _budgetAccountRepository.Save(budgetaccount);
}
var linkedSubBudgetEntries = _budgetEntryRepository.GetBudgetLineEntries(subItem.BudgetId);
foreach (var lineEntryItem in linkedSubBudgetEntries)
{
var lineEntries = new Notes_Line_Entries
{
EntryLineId = lineEntryItem.EntryLineId,
fkiAccountId = lineEntryItem.fkiAccountId,
fkiNotesColumnId = lineEntryItem.fkiNotesColumnId,
Value = lineEntryItem.Value,
isNewEntry = lineEntryItem.isNewEntry,
budgetId = getSubBudgetId
};
_budgetEntryRepository.SaveDuplicateLineEntries(lineEntries, subItem.BudgetId);
}
}
}
Success("Successfully duplicated " + model.BudgetName);
}
return RedirectToAction("BudgetListInfo", new { searchTerm = String.Empty }); //This ActionResult being redirected to, returns a PartialView - This is done so that the items in the Table are returned in correct order(i.e. Parent, then children under the parent and children under the child items)
}
复制功能:
private MasterBudget Duplication(MasterBudget model, string dupName)
{
return new MasterBudget
{
BudgetId = 0,
BudgetName = model.BudgetName + " " + dupName,
BudgetTracker = new Guid(),
FinancialYear = model.FinancialYear,
Description = model.Description,
Status = model.Status,
CreatedDate = DateTime.Now,
CurrentWorkerId = CurrentUser.Id,
OrganisationId = model.OrganisationId,
BudgetTypeName = model.BudgetTypeName,
ParentMasterBudgetId = model.ParentMasterBudgetId,
Type = model.Type,
CurrentWorkFlowTypeId = model.CurrentWorkFlowTypeId,
ParentBudgetId = model.ParentBudgetId
};
}
我读过很多帖子,说需要在客户端完成一些事情,才能真正传递值,就像使用模型一样,只有在提交(服务器端)时才起作用——但我不知道如何做到:(我确定我只是太傻了,缺少一个基本的基础来让它工作
非常感谢您的帮助!@Ajax.ActionLink()
是Razor代码,在服务器上进行解析,以便新建{…dupName=@Model.Duplicatename}
在将模型发送到浏览器之前,将dupName
的值设置为属性Duplicatename
的原始值。它不会因为文本框而神奇地改变。您需要使用javascript/jquery响应客户端事件
如果您使用$.ajax()
方法而不是ajax.ActionLink()
,这将更容易。您不清楚为什么要更改默认的id
属性,我建议只使用文本框
@Html.TextBoxFor(m => m.Duplicatename, new { @class = "form-control", @placeholder = "Enter duplicate name" })
然后在循环中,创建一个链接并将项目属性添加为data
attributes
@foreach (var item in Model.MasterBudgets)
{
if (User.IsInRole("Super Admin"))
{
<a href="#" class="duplicate btn btn-sm btn-black btn-outline" data-id="@item.BudgetId" data-type="@item.Type">Duplicate</a>
}
}
作为补充说明,您的方法应该初始化模型并返回部分视图,而不是使用
返回重定向到操作(..);
@Ajax.ActionLink()
是razor代码,在发送到视图之前在服务器上进行解析(参数值是模型发送到视图之前的原始值)。如果你想发回文本框的值,请使用表单并提交它。你的意思是我必须在我调用的ActionResult上使用FormCollection吗?如果是,我尝试过,但FormCollection没有返回值。不幸的是,我需要使用@Ajax.ActionLink来传递值-那么我可以用什么方法来实现?似乎这是一个si类似的情况,但传递长数字而不是字符串:。基本上使用$('#占位符')。使用AJAX加载(url,{[passedarguments]})
更方便。不。我的意思是使用AJAX.BeginForm()
(而不是AJAX.ActionLink()
)并将其标记为FormMethod,Get
如果您想要一个Get,但是如果您想要多个链接,其中BudgetId
和Type
的值不同,但只有一个文本框,那么您需要使用javascript更新链接的href
属性(在这种情况下,请忘记Ajax
并使用$.Ajax()
MethodsHanks。我尝试了上述方法,但控制器甚至没有被调用。我在开发人员模式下检查,控制台显示此错误:ReferenceError:$未定义。此时,我不想删除重定向(我知道你说这很愚蠢和疯狂),但是我没有太多的时间来完成这个项目,并且担心重新做每件事都会花费太长的时间:(你是说它没有被调用?这个脚本在视图中还是在一个单独的js文件中?你在浏览器控制台中遇到了什么错误?在视图中。我的意思是说ActionResult没有被调用(断点没有被命中)。我编辑了我的评论,并在开发人员模式的控制台中看到了一个错误,这意味着您没有在视图中包含jquery-{version}.js
,但这毫无意义,因为Ajax.ActionLink()
永远不会工作。我只能假设您在jquery
(您的脚本应该在关闭标记之前)
@foreach (var item in Model.MasterBudgets)
{
if (User.IsInRole("Super Admin"))
{
<a href="#" class="duplicate btn btn-sm btn-black btn-outline" data-id="@item.BudgetId" data-type="@item.Type">Duplicate</a>
}
}
var container = $('#listofBudgets');
var url = '@Url.Action("Duplicate", "Budget")';
var textbox = $('#Duplicatename');
// handle the click event of the links
$('.duplicate').click(function() {
// get the values to be sent to the method
var id = $(this).data('id');
var type = $(this).data('type');
var name = textbox.val();
$.get(url, { id: id, type: type, dupName: name }, function(data) {
container.html(data); // update the DOM
});
});