Partial View not displaying - asp.net

I don't know what is the reason but my partial view is not displaying, I have tried with other view but problem remains same. I am calling this Partial View using #Ajax.ActionLink, I have put debug points, so it clearly shows that it goes to controller method and successfully return Partial View but it is not displaying.
View(Here I am calling Controller Method):
<li class="nav-menu">#Ajax.ActionLink("Request for Change MobileNumber and EmailId", "ChangeMob", "Admin", new AjaxOptions { UpdateTargetId = "result" })</li>
Controller Method:
public ActionResult ChangeMob()
{
if (Request.IsAjaxRequest())
{
var model = new ChangeMob();
var db = new clubDataContext();
var list2 = (from s in db.tblChanges
select new ChangeMob()
{
id= s.Id,
oldno = s.Old_CNo,
newemail = s.New_Email,
oldemail = s.Old_Email,
mobileoremail = s.Mob_or_Email,
newno = s.New_CNo
}).ToList();
ViewBag.Request = list2;
return PartialView("ChangeMob", model);
}
else
return RedirectToAction("Index", "home");
}
Partial View:
#model IEnumerable<club.Models.ChangeMob>
<h2>Hello there!</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.oldno)
</th>
<th>
#Html.DisplayNameFor(model => model.newno)
</th>
<th>
#Html.DisplayNameFor(model => model.oldemail)
</th>
<th>
#Html.DisplayNameFor(model => model.newemail)
</th>
<th>
#Html.DisplayNameFor(model => model.mobileoremail)
</th>
<th></th>
</tr>
#foreach (var item in ViewBag.Request)
{
<tr>
<td>
#item.oldno
</td>
<td>
#item.newno
</td>
<td>
#item.oldemail
</td>
<td>
#item.newemail
</td>
<td>
#item.mobileoremail
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.id }) |
#Html.ActionLink("Details", "Details", new { id=item.id }) |
#Html.ActionLink("Delete", "Delete", new { id=item.id })
</td>
</tr>
}
</table>

I see one mistake in your code.
In ChangeMob Action you returns model of ChangeMob type but partial view has another type:
#model IEnumerable<club.Models.ChangeMob>
you should change it to
#model club.Models.ChangeMob
As for partial view displaying. Did you include "jquery.unobtrusive-ajax.min.js" library for using Ajax.ActionLink?
like this:
<script src="#Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

Make your controler return type from public ActionResult ChangeMob() to public PartialViewResult ChangeMob() and check
And make sure that targetID "result" is not set to dispaly:none
Mark it as answered if it work .

Related

how to get the username value from url in asp.net

i want get the username from URL i attach the pic when button click URL is change i want to get username value in variable. Kindly help me
<table class="table table-bordered table-striped">
<tr>
<th>
User
</th>
</tr>
#foreach (User item in Model.user_get)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.UserName)
<input type="button" value="Add User" onclick="location.href='#Url.Action($"Create/username={item.UserName}", "addgroupmember")'" />
</td>
</tr>
}
</table>
public ActionResult Create()
{
dynamic dy = new ExpandoObject();
dy.user_get = get_user();
Uri MyUrl = Request.Url;
Console.WriteLine(MyUrl);
string name = Request.QueryString["username"];
List<string> selectedUsers = new List<string>();
selectedUsers.Add(name);
return View(dy);
}
enter image description here
var qsArray = Request.QueryString.AllKeys.Select(key => new { Name = key.ToString(), Value = Request.QueryString[key.ToString()] }).Where(a => !string.IsNullOrEmpty(a.Value)).ToArray();
var UserName= qsArray.Where(a => a.Name == "username").Select(a => a.Value).ElementAt(0);

ASP.NET unable to search encrypted table

I have an encrypted database, I am encrypting it using this StringCipher done by CraigTP on this post.
However when I try to search my database I am unable to search using Decrypted values, Since every value i encrypt is different, encrypting the search value and trying to match it to the database is useless. Now I'm decrypting the list and trying to match the search value to this decrypted list, but I still can't get results to appear. However If I search for the encrypted value grabbed directly from the DB I do get the results. I've tried everything I can think of and I'm out of ideas.
Here is my index method:
public ViewResult Index(string sortOrder, string searchString)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Username" : "";
ViewBag.CurrentSort = sortOrder;
var Users = from s in db.Users
select s;
foreach(User element in Users)
{
element.Username = StringCipher.Decrypt(element.Username.ToString());
element.Password = StringCipher.Decrypt(element.Password.ToString());
}
if (!String.IsNullOrEmpty(searchString))
{
Users = Users.Where(s => s.Username.Contains(searchString));
}
switch (sortOrder)
{
case "Username":
Users = Users.OrderByDescending(s => s.Username);
break;
}
return View(Users.ToList());
}
And here is my Index view:
#model IEnumerable<EncryptTest.Models.User>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
#using (Html.BeginForm())
{
<p>
Find by name: #Html.TextBox("SearchString")
<input type="submit" value="Search" /></p>
}
<table class="table">
<tr>
<th>
#Html.ActionLink("Username", "Index", new { sortOrder = ViewBag.NameSortParm })
</th>
<th>
Password
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Username)
</td>
<td>
#Html.DisplayFor(modelItem => item.Password)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.ID_User}) |
#Html.ActionLink("Details", "Details", new { id = item.ID_User }) |
#Html.ActionLink("Delete", "Delete", new { id = item.ID_User })
</td>
</tr>
}
</table>

ASP.Net MVC 5 Load list data via ajax

I have a list in asp.net mvc 5
I have limited the number of records to be displayed in page.
now on scroll I do ajax call, the first call works fine but when I scroll down more it recall the function repeatedly 5 to 10 times and lost it again display the data, it was really strange, I could not find solution
My Controller:
public ActionResult Index()
{
int starting = 0;
if (Request.Form["starting"] != null)
{
starting = Convert.ToInt32(Request.Form["starting"]);
}
int takes = 15;
if (Request.Form["takes"] != null)
{
takes = Convert.ToInt32(Request.Form["takes"]);
}
//string strpost = "&ajax=1";
var query = db.MyEmployee.ToList().Skip(starting).Take(takes);
if (Request.IsAjaxRequest())
{
starting = starting+15;
query = db.MyEmployee.ToList().Skip(starting).Take(takes);
ViewData["starting"] = starting;
ViewBag.takes = takes;
return PartialView("_PartialIndex",query);
}
ViewBag.starting = starting;
ViewBag.takes = takes;
return View(query);
}
My Model:
public class Employee
{
public int Id { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
}
My View and partial view code:
<div id="mypage">
#model IEnumerable<MVC5WAuth.Models.Employee>
#{
ViewBag.Title = "Index";
}
<h2>Index 1</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Id)
</th>
<th>
#Html.DisplayNameFor(model => model.FullName)
</th>
<th>
#Html.DisplayNameFor(model => model.Email)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Id)
</td>
<td>
#Html.DisplayFor(modelItem => item.FullName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
#Html.ActionLink("Details", "Details", new { id = item.Id }) |
#Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
<script type="text/javascript">
$(window).scroll(function () {
if ($(window).scrollTop() == $(document).height() - $(window).height()) {
// ajax call get data from server and append to the div
var ajax_image = "<img src='../Content/loading.GIF' >";
$('#mypage').html(ajax_image);
var params = '&starting=' + #ViewBag.starting + '&takes=' + #ViewBag.takes;
$.ajax({
url:'#Url.Action("Index", "Employees")',
type: "POST",
data: params,
})
.done(function (r) {
$('#mypage').html(r);
});
}
});
</script>
You current code is just replacing the existing view each time you scroll and make a ajax call, not updating the existing view with the next set of rows you want. You code also has some inefficiencies such as materializing all your records to an in-memory set before calling .Skip() and .Take().
You need to break this into 2 separate methods and views, one to generate the initial view, and one to return a partial of just the records you want to append to the main view.
Controller
public ActionResult Index()
{
return View();
}
public ActionResult Fetch(int startIndex)
{
query = db.MyEmployee.OrderBy(x => x.ID).Skip(startIndex).Take(15);
return PartialView(query);
}
Index.cshtml
#model IEnumerable<MVC5WAuth.Models.Employee>
....
<table class="table">
<thead>
<tr>
<th>#Html.DisplayNameFor(model => model.Id)</th>
....
</tr>
</thead>
<tbody id="tbody">
#{ Html.RenderAction("Fetch", new { startIndex = 0 }); } // generate the 1st 15 rows
</tbody>
</table>
<script type="text/javascript">
var start = 15;
var url = '#Url.Action("Fetch")';
var tbody = $('#tbody');
$(window).scroll(function () {
if ($(window).scrollTop() == $(document).height() - $(window).height()) {
....
$.get(url, { startIndex: start }, function(response) {
tbody.append(response);
start += 15; // increment for next call
});
}
});
</script>
Fetch.cshtml
#model IEnumerable<MVC5WAuth.Models.Employee>
#foreach (var item in Model)
{
<tr>
<td>#Html.DisplayFor(m => item.Id)</td>
....
</tr>
}

How to retrieve form data and assign it to a text as a url on MVC 4

i would like to return below url from a search submit where id will be get from database.
So when a user search something by id it will search my database and display the result on my home view. then i want to transform my ID a clickable url which is this one:
http://myadress.com:8787/Handlers/DataExport.ashx?format=pdf&id=???&direction=0
Any idea how to do?
This is my home view:
<body>
<p>
#using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
<b>SEARCH BY:</b> #Html.RadioButton("searchby", "ID", true) <text>ID</text>
#Html.RadioButton("searchby", "NAME") <text>NAME</text>
<br /><br />
#Html.TextBox("search") <input type="submit" value="SEARCH" />
}
</p>
<table>
<tr>
<th>
ID
</th>
<th>
NAME
</th>
<th>Actions</th>
</tr>
#if (Model.Count() == 0)
{
<tr>
<td colspan="2">NO DATA FOUND.</td>
</tr>
}
else
{
foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.ID)
</td>
<td>
#Html.DisplayFor(modelItem => item.NAME)
</td>
<td>
#Html.ActionLink("Details", "Details", new { id = item.id })
</td>
</tr>
}
}
this is my controller:
public class HomeController : Controller
{
private mydbEntities db = new mydbEntities();
//
// GET: /Home/
public ActionResult Index(string searchBy, string search)
{
if (searchBy == "ID")
{
return View(db.mytable.Where(x => x.ID == search).ToList());
}
else (searchBy == "NAME")
{
return View(db.mytable.Where(x => x.NAME == search).ToList());
}
}
You could just create an anchor tag and substitute the id in the href attribute
your link text
you have to use jquery for this. something like
$('.btnSearch').on('click', function(){
$('.lnkSubmit').attr('href', '#Url.Action("Action", "Controller", new { id = "----" })'.replace("----", (returned id here));
});
this will replace the url of a link with class lnkSubmit and will include the id that you put in it. Let me know if you have any questions.
In my Blog application, this is how I implemented search functionality for searching posts.
Partial view for search:
#using (Html.BeginForm("Search", "Blog", FormMethod.Get, new {id = "search-form"}))
{
<input type="text" name="s" placeholder="Search">
<button type="submit">Go</button>
}
Search action in controller:
public ActionResult Search(string s)
{
var model = _dbPostRepository.GetPostsForSearch(s);
ViewBag.TotalPosts = _dbPostRepository.TotalSearchPosts(s);
return View("Posts");
}
Posts View:
#model FirstBlog.Core.Post
#if (#ViewBag.TotalPosts > 0)
{
foreach (var item in Model)
{
Html.RenderPartial("_PostTemplate", item);
}
}
else
{
<p>No posts found!</p>
}
_PostTemplate is the view for each post. Hope this would help.

Custom Filename for #Html.EditorForModel()?

so I have the following code:
#model IEnumerable<My_School_Book.Models.Mark>
#{
ViewBag.Title = "Marks";
}
<div class="row-fluid">
<h1>Assignment Marks:</h1>
#using (Html.BeginForm("Marks", "Assignment", FormMethod.Post, new { #class = "form-horizontal" }))
{
<table class="table table-bordered">
<thead>
<tr>
<th>Student</th>
<th style="width: 50px;">Mark
</th>
<th style="width: 50px;">Total
</th>
<th style="width: 50px;">Weight</th>
</tr>
</thead>
<tbody>
#Html.EditorForModel()
</tbody>
</table>
<div class="form-actions">
<button type="submit" class="btn btn-primary"><i class="icon-checkmark"></i> Save</button>
<i class="icon-back"></i> Cancel
</div>
}
</div>
As you can see my Model being passed into the view is an IEnumerable of the Model Mark.
Now I have a EditorTemplate under the Views/Shared folder called Mark:
#model My_School_Book.Models.Mark
<tr>
<td>#Model.Student.FullName</td>
<td>
#Html.TextBoxFor(model => Model.Grade, new { #class = "span12", #style = "min-height: 20px;", #maxlength = "5" })
</td>
<td>
#Model.Assignment.Total
</td>
<td>#Model.Assignment.Weight</td>
</tr>
My question is, can I rename my EditorTemplate file from Mark to AssignmentMarks? I'd like it to be more specific instead of a generic name called Marks.
Edit:
Inside my MarkRepository.cs
public IEnumerable<Mark> GetAllByAssignmentID(Guid id)
{
return context.Marks.Where(m => m.AssignmentID == id);
}
Inside my AssignmentController.cs
public ActionResult Marks(Guid id)
{
IEnumerable<Mark> Marks = markRepository.GetAllByAssignmentID(id).ToList();
return View(Marks);
}
[HttpPost]
public ActionResult Marks(IEnumerable<Mark> viewModel)
{
if (ModelState.IsValid)
{
}
return View(viewModel);
}
Inside my Marks.cshtml
#Html.EditorForModel("Mark", "AssignmentMark")
Inside my AssignmentMark.cshtml
#model My_School_Book.Models.Mark
<tr>
<td>#Html.DisplayTextFor(model => model.Student.FullName)</td>
<td>#Html.TextBoxFor(model => model.Grade, new { #class = "span12" })</td>
<td>#Html.DisplayTextFor(model => model.Assignment.Total)</td>
<td>#Html.DisplayTextFor(model => model.Assignment.Weight)</td>
</tr>
The error I receive on my View is now:
System.Data.Entity.DynamicProxies.Assignment_7EDE7AD8477AB363802A036747CD8A8B259C6CD72DCEF587A2B0FBEDC7B2DCE1System.Data.Entity.DynamicProxies.Assignment_7EDE7AD8477AB363802A036747CD8A8B259C6CD72DCEF587A2B0FBEDC7B2DCE1System.Data.Entity.DynamicProxies.Assignment_7EDE7AD8477AB363802A036747CD8A8B259C6CD72DCEF587A2B0FBEDC7B2DCE1System.Data.Entity.DynamicProxies.Assignment_7EDE7AD8477AB363802A036747CD8A8B259C6CD72DCEF587A2B0FBEDC7B2DCE1
Rename your model class to be AssignmentMarks
Instead of #EditorForModel(), run the following:
#foreach(var mark in Model)
{
#EditorFor(m => m.Mark, "AssignmentMark")
}
Hope this will help you.

Resources