MVC视图-> JavaScript->控制器方法-> Javascript-> Div标签 [英] MVC View-> JavaScript->Controller method->Javascript->Div tag
问题描述
我有一个创建,编辑,删除应用程序.在我的索引视图中,有用于编辑的按钮.通过单击此按钮,它将打开弹出窗口,其中应显示所有数据以进行编辑.
I have a create,edit,delete application On my Index view i have Button for Edit. by clicking this button it should open pop up in which all data should be displayed for editing.
要实现此目的,我传递了正在编辑的那一行的ID.参见下面的代码:
To achieve this i passed ID of that row which is getting Edited. see code below :
<td>
<button type="button" onclick="EditPopup(@item.Id)">Edit</button>
</td>
在这里,我正在将ID传递给我的EditPopup javascript方法.参见下面的方法:
here i am passing ID to my EditPopup javascript method. see the method below :
<script type="text/javascript">
$(document).ready(function () {
$("#EditDialog").dialog({
autoOpen: false,
title: 'Title',
width: 'auto',
height: 'auto',
modal: true
});
});
function EditPopup(Get_Id) {
alert(Get_Id) // I am getting correct ID here.
$.ajax({
method: "POST",
url: '@Url.Action("Edit","Home")',
dataType: 'json',
cache: false,
data:{Get_Id}, // tried : {id:Get_Id} , {id:"Get_Id"} not working
success: function (data) {
$('#EditDialog').html(data);
}
});
$("#EditDialog").dialog("open");
}</script>
我正在将ID的值发送到我的Controller方法.这就是为什么我在ajax调用中使用Post方法的原因. Edit是方法的名称,Home是控制器的名称.
I am sending value of ID to my Controller method Edit thats why i am using Post method in ajax call. Edit is name of method and Home is name of controller.
HomeController编辑方法
[HttpPost]
public JsonResult Edit(int? id)
{
FloorFactor floorFactor = db.FloorFactors.Find(id);
return Json(floorFactor, JsonRequestBehavior.AllowGet);
}
// POST:
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
FloorFactor floorFactor = db.FloorFactors.Find(id);
return View(floorFactor);
}
在一些示例中,我看到在ajax调用中,它们通常使用json结果方法.所以这就是我也使用json结果方法的原因.
in few examples i saw that in ajax call they usually use json result method. so that is the reason i also used json result method.
最后在我的索引视图中显示弹出结果的代码.
finally Code which is in my index view where i will show pop up result.
<div id="EditDialog" style="display:none;">
<label> Floor Factor </label>
<br />
<label> Effective From :</label>
所以整个场景是:
我将按钮点击事件的ID值发送给javascript.
在javascript上,我调用了我的控制器方法以获取数据.
这些应该在div的EditDialog框中传递.
在div块上,它应该显示为弹出窗口.
I send id value on button click event to javascript.
on javascript i make a call to my controller method to get data.
those should pass in EditDialog box of div.
on div block it should display in pop up.
当前输出:
我还想了解ajax调用中url字段的工作方式.
如果我将多列结果作为该URL的输出,如何在ajax调用的Data部分中收集所有输出.
请同时说明成功后我可以在函数中传递哪些参数.
I also want to understand how url field works in ajax call.
if i am getting multiple column results as output of that url how can i collect all output in Data part of ajax call.
please also explain on success what parameters i can pass in function.
感谢您的解释和帮助.
控制台"选项卡上没有显示错误. 如该脚本标签中所示,我认为它正在发送请求,因为它生成了请求ID.
Edit : It shows no error on console tab. as shown in this script tab i think it is sending a request as it generates request Id.
推荐答案
尝试以下更改
操作代码:
[HttpPost]
public JsonResult Edit(int? id)
{
FloorFactor floorFactor = db.FloorFactors.Find(id);
return Json(floorFactor, JsonRequestBehavior.AllowGet);
}
查看更改
<div id="EditDialog" style="display:none;">
<label> Floor Factor </label> <span id="floorFactor"></span>
<br />
<label> Effective From :</label> <span id="effectiveFrom"></span>
成功的方法更改
if(data)
{
// GET VALUES
var floorFactor = data.Property_Having_FloorFactor;
var effectiveFrom = data.Property_Having_EffectiveFrom;
// ASSIGN VALUES
$('#floorFactor').text(floorFactor);
$('#effectiveFrom ').text(effectiveFrom );
// OPEN DIALOG
$("#EditDialog").dialog("open");
}
希望它会为您工作.
这篇关于MVC视图-> JavaScript->控制器方法-> Javascript-> Div标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!