JQuery Popup for Deletion

JQuery Popup for Deletion

Below are the steps :

1. Add a class “delete” to the delete link i.e. @class=”delete”
2. Add some needed attributes like @id,@name etc.
3.Download jquery.alerts.js & jquery.alerts.css and add them to your scripts folder in your project
4.Add a method in your controller with the name Deleteme like

[HttpPost]
public JsonResult Deleteme(int Id)
{
// your logic
return Json("Your Message");
}

5. Below is the code to be added in your View.


$(document).ready(function () {
$('a.delete').click(OnDeleteClick);
});

function OnDeleteClick(e) {
var Name = e.target.name;
var Id = e.target.id;
var flag = jConfirm('Are you sure you want to delete ' + Name + ' ?', "Ok", "Cancel", function (r) {
if (r) {
$.ajax({
url: '/(ControllerName)/Deleteme',
type: 'POST',
data: { Id: Id },
dataType: 'json',
success: function (result) {
jAlert(result, "Ok", function (r) { if (r) { window.location.reload(true);}}); $("#" + Name).parent().parent().remove();
},
error: function () { jAlert("Error!","Ok", function (r) { if (r) { location.reload(true);}}); }
});
}
})
return false;
}

@Html.ActionLink("Delete", "Delete", "(NameOFTheFolderOfViews)", new { @class = "delete", @id = item.Id, @name = item.Name }) )));

Get the Text From Html.DropDownListFor

Get the Text From Html.DropDownListFor

You may not get this easily with the default model binding. You have to a small workaround like this.

1) Add a new property to your model/viewmodel to store the selected text


public class DocumentModel
{
public int TypeID { get; set; }
public List DocumentTypes { get; set; }
public string SelctedType { set;get;}
}

2) Use Html.HiddenFor Helper method to create a hidden variable in the form for this property


@Html.HiddenFor(x => x.SelctedType)

3) Use little javascript to override the submit ! ie; When user submits the form, Get the selected Text from the dropdown and set that value as the value of the Hidden field.


$(function () {
$("form").submit(function(){
var selTypeText= $("#TypeID option:selected").text();
$("#SelctedType").val(selTypeText);
});
});

Now in your HTTPPost action method, This will be available in the SelectedType property.


[HttpPost]
public void UploadDocument(DocumentModel model)
{
if(ModelState.IsValid)
{
string thatValue=model.SelectedType;
}
}

Ref : Get the Text From Html.DropDownListFor