आप पॉपअप के लिए jquery UI Dialog का उपयोग कर सकते हैं। चलो यहाँ एक छोटा सा सेटअप है।
public class MyViewModel
{
public string ValueId { get; set; }
public IEnumerable<SelectListItem> Values
{
get
{
return new[]
{
new SelectListItem { Value = "1", Text = "item 1" },
new SelectListItem { Value = "2", Text = "item 2" },
new SelectListItem { Value = "3", Text = "item 3" },
};
}
}
public string NewValue { get; set; }
}
एक नियंत्रक:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
return Content("thanks for submitting");
}
}
और एक (~/Views/Home/Index.aspx
) विचार:
<%@ Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<AppName.Models.MyViewModel>"
%>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm()) { %>
<%= Html.DropDownListFor(x => x.ValueId, Model.Values) %>
<br/>
<%= Html.EditorFor(x => x.NewValue) %>
<%= Html.ActionLink("Suggest Value", "New", "NewValue", null, new { id = "new-value-link" }) %>
<button type="submit">OK</button>
<% } %>
<div id="dialog"></div>
</asp:Content>
तो हम ले सकता है
हम मुख्य रूप के लिए एक दृश्य के मॉडल के लिए होता है पॉपअप की देखभाल करें। हम इसके लिए एक दृश्य के मॉडल को परिभाषित:
public class NewValueViewModel
{
public string Foo { get; set; }
public string Bar { get; set; }
}
एक नियंत्रक:
public class NewValueController : Controller
{
public ActionResult New()
{
return PartialView(new NewValueViewModel());
}
[HttpPost]
public ActionResult New(NewValueViewModel model)
{
var newValue = string.Format("{0} - {1}", model.Foo, model.Bar);
return Json(new { newValue = newValue });
}
}
और एक इसी आंशिक दृश्य (~/Views/NewValue/New.ascx
):
<%@ Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.NewValueViewModel>"
%>
<% using (Html.BeginForm(null, null, FormMethod.Post, new { id = "new-value-form" })) { %>
<%= Html.EditorFor(x => x.Foo) %>
<%= Html.EditorFor(x => x.Bar) %>
<button type="submit">OK</button>
<% } %>
अब सभी अब सिर्फ़ एक सा लिखना है सब कुछ तार करने के लिए जावास्क्रिप्ट का।
<script src="<%: Url.Content("~/Scripts/jquery-1.5.1.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery-ui-1.8.11.js") %>" type="text/javascript"></script>
और एक कस्टम जावास्क्रिप्ट फ़ाइल है कि हमारे कोड में शामिल होंगे:: हम jQuery और jQuery ui शामिल
$(function() {
$('#new-value-link').click(function() {
var href = this.href;
$('#dialog').dialog({
modal: true,
open: function (event, ui) {
$(this).load(href, function (result) {
$('#new-value-form').submit(function() {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (json) {
$('#dialog').dialog('close');
$('#NewValue').val(json.newValue);
}
});
return false;
});
});
}
});
return false;
});
});
यह कोशिश की, लेकिन एटीएम, बजाय इसके बारे में अप पॉपिंग, यह सिर्फ खोलता है एक नया पृष्ठ। सोच रहा था, jquery का कौन सा हिस्सा मोडल ट्रिगर करता है? – gdubs
यह लिंक की '.click() 'घटना की सदस्यता है।सुनिश्चित करें कि आपने सभी 3 जावास्क्रिप्ट फ़ाइलों ('jquery-1.5.1.min.js', 'jquery-ui-1.8.11.js' और' my.js') में सही ढंग से शामिल किया है - जिसमें मैंने जो स्क्रिप्ट दिखाया है वह ऑर्डर) और जावास्क्रिप्ट कंसोल में कोई त्रुटि नहीं है। –
समस्या को देखा, यह एक अतिरिक्त '} का cuz था);' जबरदस्त हंसी! एक बार फिर धन्यवाद! – gdubs