PartialView Navigation not returning parameters - c#

I've setup a partialview to handle navigation throughout multiple views. Some of these views use a different model so I'm passing that model in like this
#Html.Partial("~/Views/Navigation/_PartialTabs.cshtml", new xxx.OpenAccess.OBProfiles())
It load up my partialview just fine
#using xxx.Helpers;
#model xxx.OpenAccess.OBProfiles
<ul class="nav nav-tabs">
<li role="presentation" class="#Html.IsActive("Edit", "OBProfile")">#Html.ActionLink("Edit", "Edit", "OBProfile", new { id = Model.ProfileID }, null)</li>
<li role="presentation" class="#Html.IsActive("Index", "OBProfileTasks")">#Html.ActionLink("Tasks", "Index", "OBProfileTasks", new { id = Model.ProfileID }, null)</li>
<li role="presentation">Messages</li>
However when I mouse over the links, the parameters (Model.ProfileID) return 0 regardless of what screen i'm on. So the tab URLs look like this http://localhost:55129/OBProfileTasks/Index/0
What am I missing that it isn't returning the /Number of whatever profileid ive selected?

You need to prepopulate OBProfiles. Here are two ways:
Constructor:
In the OBProfiles class add:
public OBProfiles(){
ProfileID = *some value*;
}
Static method:
public static OBProfiles GetProfiles(){
return new OBProfiles{ProfileId = *Some value*};
}
The for static method call:
#Html.Partial("~/Views/Navigation/_PartialTabs.cshtml", OBProfiles.GetProfiles())

I believe TheDizzle hit the nail on the head. When you pass the model in as new, you're passing in a clean version of that model. Try just passing the model in without using the new keyword.

Related

Reload partial view using html razor

I have this simple list of users in my model.
If we click one user, I would like to set that user as the chosen one and refresh the partial view.
My code looks like:
<div id="myPartialView">
#if (#Model.ChosenUser != null)
{
#Model.ChosenUser.UserName
}
<ul>
#foreach (var u in Model.Users)
{
<li>
<a href='#Url.Action("ChooseUser", "Controller", new { userId = u.UserId })'>#u.UserName</a>
</li>
}
</ul>
The controller method returns an Ok();
My current code redirects me to an empty page and I have to go back and refresh the page in order to see the model changes.
My question is, how can I refresh only this partial view after the razor action?
You will need to use Ajax.ActionLink here :
#foreach (var u in Model.Users)
{
<li>
#Ajax.ActionLink(u.UserName, // <-- Text to display
"Choose User", // <-- Action Name
"Controller", // <-- Controller Name
new AjaxOptions
{
UpdateTargetId="myPartialView", // <-- DOM element ID to update
InsertionMode = InsertionMode.Replace, // <-- Replace the content of DOM element
HttpMethod = "GET" // <-- HTTP method
})
</li>
}
The helper method would render the html needed to create the anchor tag element with the specified values, and you need to make sure that you have scripts added in the master layout for unobtrusive ajax.
For that you can look here, what scripts are needed to be pre-requisite:
How to use Ajax.ActionLink?
and your action method should be returning the model of the same type that your partial view expects with data populated in the model.
Please refer to the following post to learn in detail about Ajax Action Link:
http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/ajax-actionlink-and-html-actionlink-in-mvc/

The parameters dictionary contains a null entry for parameter

I'm attempting to, on the click of a button, complete a to-do item, therefore removing it from the list.
I'm using an ActionLink for the button:
#foreach (var item in Model)
{
<li class="#item.Priority">
#item.Text
<div class="agile-detail">
#Html.ActionLink("Done", "Complete", "Home", new { id = item.ToDoId }, new { #class = "pull-right btn btn-xs btn-primary" })
<i class="fa fa-clock-o"></i> #item.Date
</div>
</li>
}
And a very short action for the processing in the controller:
public ActionResult Complete(int todoId)
{
using (var db = new KnightOwlContext())
{
DashboardHelper dashboardHelper = new DashboardHelper(db);
dashboardHelper.CompleteToDo(todoId);
return RedirectToAction("Index", "Home");
}
}
Clicking the button generated a URL of:
http://site/Home/Complete/1
I've looked up a solution and so far it looks like it could be any number of issues. Also the ActionLink button is inside a partial view so I'm not sure if that changes anything in terms of incorrect routing setup? For routing too I'm just using the default config that comes with an MVC project in Visual Studio.
Just having trouble narrowing down the cause of the issue so where to check first?
The parameter in your method is int todoId but you not passing any value for that - your only passing a value for a parameter named id.
Change the method to
public ActionResult Complete(int id)
or change the link to use new { todoId = item.ToDoId }, but that will add a query string value, not a route value, unless you create a specific route definition with url: "Home/Complete/{todoId}"

Razor sharp html link differ from the executed link

While creating a custom pager I came across the problem when I send an string to my view and I encode it as #raw(strHTML) it will automatically add a controller name in front of all my links. On initial load the pager is loaded correctly and no extra controllername was added. When I hit the next button a get request is done to the action and the next page has to be loaded and this will also create a new pager. The outputted html is exactly the same as the first time this was executed. The html that is created by my customPager while debugging:
<ul>
<li class='previousPage'><span>Previous</span></li>
<li class='currentPage'><span>1</span></li>
<li><a title='Next page' rel='next nofollow' href='Invoices/Index?page=2'>2 </a></li>
<li><a title='Next page' rel='next nofollow' href='Invoices/Index?page=3'>3 </a></li>
<li><a title='Next page' rel='next nofollow' href='Invoices/Index?page=4'>4 </a></li>
<li><a title='Next page' rel='next nofollow' href='Invoices/Index?page=5'>5 </a></li>
<li class='nextPage'><a title='Volgende pagina' rel='next nofollow' href='Invoices/Index?page=2'>Volgende</a></li>
</ul>
The html is correct, but when the page is rendered and I hover over the link it reproduces the following link:
localhost:xxxx/company/Invoices/Invoices/Index?page=1
company is the area, Invoices the controller , second Invoices (NOT necessary, this breaks the link), index the action name.
I was wondering how the html and the reproduced link while clicking in the browser can be different.
Thanks in advance
Do not hardcode the href property value, Use the Url.Action helper method instead. It will fix your problem.
Replace
href='Invoices/Index?page=2'
with
href='#Url.Action("Index","Invoices",new { page=2 })'
EDIT:(As per the comment) :
If you want to use Url.Action method in your custom class
Pass the RequestContext to your custom class from the controller. I would add a Constructor to your custom class to handle this.
using System.Web.Mvc;
public class PaginationCustom
{
private UrlHelper _urlHelper;
public PaginationCustom(UrlHelper urlHelper)
{
_urlHelper = urlHelper;
}
public string GetPagingMarkup()
{
//add your relevant html markup here
string html = "<div>";
string url = _urlHelper.Action("Index", "Invoices", new { id = 3 });
html= html+"<a href='"+url + "'>3</a></div>";
return html;
}
}
You need to import System.Web.Mvc namespace to this class to use the UrlHelper class.
Now in your controller, create an object of this class and pass the controller context,
UrlHelper uHelp = new UrlHelper(this.ControllerContext.RequestContext);
var paging = new PaginationCustom(uHelp );
//Now call the method to get the Paging markup.
string pagingMarkup = paging.GetPagingMarkup();

<$G+$> Strange characters appearing in ASP.NET MVC5 Helper

My helper in the view, which is intended to display the full name of a user who is registered in the application, and the username if logged in via 3rd party authentication.
#using MyApp.Models;
#helper GetUserName()
{
if (User.Identity.AuthenticationType == "Application")
{
using (var db = new MyAppDbContext())
{
#db.Users.Find(User.Identity.GetUserId()).FullName
}
}
else
{
#User.Identity.GetUserName()
}
}
Then I use this helper:
#if (Request.IsAuthenticated)
{
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", #class = "navbar-form pull-right" }))
{
#Html.AntiForgeryToken()
<ul class="nav">
<li>
#Html.ActionLink("Hello " + GetUserName() + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
</li>
<li>Log off</li>
</ul>
}
}
The name of my user is Proba234, and it displays like this:
Why are those strange characters (<$G+$>) appear, and how to get rid of them?
It is probably some kind of feature or bug related with usage of helpers within Visual Studio Page Inspector, you probably won't see those tags under external browser. I reproduced it easily in VS 2013. A thread about it can be found for example on ASP.NET forum
It seems like those funny characters are generated by helper. The probleem seems to be that the output of the helper is not meant to be consumed as regular string. Apparently it generates some control characters, which are meant to be there for Razor, but if we try to use the output as regular string (as #Html.ActionLink() expects its argument) they become visible.
To get rid of them, I shouldn't use the GetUserName() helper inside the #Html.ActionLink(), but get the UserName in the controller, and pass it via a property of the viewmodel. I shouldn't have that kind of logic in my view anyway.

C# mvc3 Actionlink not sending parameter in URL

I am having some issues with MVC3 and Actionlink. I am dynamically building a HTML ActionLink using data passed from my model.
In the view:
#for (int i = 0; i < Model.CountriesList.Count; i++)
{
<li class="sub">#Html.ActionLink(Model.CountriesList[i].countryname,
"PageSub", new { PageID = Model.PageNo,
countryid = Model.CountriesList[i].countryid
})</li>
}
The Model.PageNo is passed from the controller and has a value. I have tested this. Every time I click on the link I am getting an error as the PageNo is not being passed as a parameter. The url outputs like below:
http://localhost/Pages/PageSub?countryid=196
I need it to be below which works when I directly enter it:
http://localhost/Pages/PageSub?PageID=136&countryid=196
This is my Controller ActionResult it directs to:
public ActionResult PageSub(int? PageID, int? countryid)
{
}
I have checked the code using developer tools and the link has been built as expected:
<li class="sub">
<a href="/Pages/PageSub?PageID=136&countryid=196">
</li>
Has anyone had similar issues? Any advice would be appreciated. Thanks
Try this:
#Html.ActionLink(Model.CountriesList[i].countryname, "PageSub", "Pages", new { PageID = Model.PageNo, countryid = Model.CountriesList[i].countryid }, null)

Categories