Ok, so I have an ASP.Net MVC 5 project. Each page can contain several "Widget" aka partial views. Now I have a button in one of the partial view and when that button is clicked I want to capture the browser URL. Here is my attempt:
Suppose I go to https://www.elloh.com/test below is the code behind.
Main Page
{
partial view one
{
// some razor code
}
partial view two
{
<div>#Request.Url.ToString()</div>
<div>#Request.Url.AbsoluteUri</div>
<button type="button" onclick="location.href='#Url.Action("Logoff", "Security")'"> LogOff</button>
}
}
Points: Now I some how want to pass the https://www.olleh.com/test when the LogOff button is clicked. I have https://www.olleh.com/test in the browser tab. I know I can pass the anonymous object. But All I get is the URL pertaining to logout partial view. So the div in the second partial view displays something like https://www.olleh.com/test/partialView2. So if I do something like below it will not work.
<button type="button" onclick="location.href='#Url.Action("Logoff",
"Security", new {id = #Request.Url.ToString()})'"> LogOff</button>
P.S: the code snippet is not correct syntax wise. Bear with me on this.
Expected output: Need to be able to capture Browser URL when button in partial view is clicked.
HttpContext.Current.Request.Url Or use Raw URL
https://msdn.microsoft.com/en-us/library/system.web.httprequest.rawurl.aspx
You can do it using HttpContext.Current.Request.UrlReferrer property.
Related
I have a common partial view (_New.cshtml) that is rendered from two different pages (Loads-1 and Loads-2) in the application. Within the partial view there is a tooltip (on hover).
The tooltip appears on hover as expected when the partial view is rendered from Loads-1. But, the tooltip does not appear when the same partial view is rendered from Loads-2.
I am new to .Net MVC and knockout.js So, am still trying to get my head around views, partial views, knockout data bindings and stuff. I tried plenty of variations, but the tooltip never seems to appear on Loads-2 but always works on the first.
Are there any specific reasons why this is happening? Will be glad in case someone has some tips to debug this issue and get the expected result.
Following is the code I have implemented for the tooltip within the partial view:
<span class="tooltip-bca" data-toggle="tooltip" data-placement="right" data-html="true" data-bind="attr: { 'data-original-title': '#if (..) { tooltip content }' }">
Hover Me
</span>
So I have a view, that has a prtial for its header, that partial can have some links to for example going back to home page, going to profile page, etc...
Sometime some of those links can be hidden or disabled based on some situations.
What I need is in one of my controller action method I need to have accees to some sort of flag or variable that tell me if that home page button is on the page or not. How can I do that ?
In your controller method you can have add a line like this in cases where you need to limit the links displayed:
ViewBag.LimitOptions = true;
Then in your partial view where you output your buttons, you can do this:
#if(ViewBag.LimitOptions != null)
{
/*define links here*/
}
In my Checkout.SelectAddress.cshtml page I have the following form:
#using (Html.BeginFormAntiForgeryPost(Url.Action("SelectAddress", "Checkout", new { area = "Store" })))
{
//extra code collecting information
<li class="align right"><button type="submit">#T("Next")</button></li>
}
I am using orchard cms 1.6 but I am having a problem with a navigation issue.
When the user clicks the above button they are directed to the URL:
http://localhost:30320/OrchardLocal/UMACS.Store/Checkout/Summary
I want to navigate to : http://localhost:30320/OrchardLocal/
i've tried replaing 'checkout' from the original form but having no joy. Anyone have any idea?
That form posts to a specific action in some module apparently named "Store". That action seems to then redirect to Summary. The only way you can make it redirect to something else is by changing its code.
Hi
i want to do a work in asp.net using mvc and ajax
i have a button
when i click that button,its text should be changed.e.g before click(Click me) after click(u clicked me)
but i want to do this work in MVC2
i have studied but couldn't understood mvc
kinfly do this example so that i can understand it easily
Best Regards:
Shaahan
are u just trying to change the text label on click?
there's a few ways to do this, but you can probably just use an onclick event and change the label straight when the user click on the button.
for example like so.
but if you want to do it MVC just for the heck of it, then you can create a view, click on the button and do a form post to the same page, and on the controller use ViewData["ButtonLabel"] and update the button label when the page goes back :P
You can do what you want simply with javascript. If you want to learn mvc here a simple [music store][1] tutorial that I has helped me a lot! [1]: http://www.asp.net/mvc/tutorials/mvc-music-store-part-1
MVC stands for Model, View, Controller.
The way this works is you have a controller, say HomeController which is a class derived from Controller. When you access /Home/ on the site through your browser it serves the browser a view and any additional information, often cookies and such. The model is the data and logic of the program, often handling things like databases.
There are multiple ways to go about this example.
//In HomeController class
public ActionResult Index()
{
return View();
}
public ActionResult Clicked()
{
return View()
}
And then for the Views for the index view you'd have a button which would link to /Home/Clicked. Then on the clicked view you'd have the button with changed text.
Of course this is only one way to do it you could just append a number do the /Home/ url and pass that to the view and if it's not 0 have the test be different or use javascript to change the button's text
Index view:
<form>
<input type="button" value="NClicked" onclick="window.location.href='/Home/Clicked'">
</form>
Clicked view:
<form>
<input type="button" value="Clicked" onclick="window.location.href='/Home/'">
</form>
Of course there's more to the views than that, but you can insert that into your body.
Let's assume I have 2 Controllers, TopicsController and PostsController.
For each controller, I have a couple of views (Index & Details).
The Topic (Index) view inherits System.Web.Mvc.ViewPage<IEnumerable<MessageBoard.Models.Topic>>
The Topic (Details) view inherits System.Web.Mvc.ViewPage<MessageBoard.Models.TopicFormViewModel>
I'm using a TopicFormViewModel because I'm sending additional data along with the Model.
The Post (Details) view just inherits System.Web.Mvc.ViewPage<MessageBoard.Models.Post>
Now, I've created a partial view (CreatePost.ascx) which is (obviously :p) used to create a new Post. I want to be able to re-use this control on all of the views you see above.
Update
I've tried rendering the partial view using <% Html.RenderPartial("New"); %> from my Topics/Index.aspx View, but that results in an exception
The model item passed into the dictionary is of type 'System.Data.Linq.Table`1[MessageBoard.Models.Topic]', but this dictionary requires a model item of type 'MessageBoard.Models.Post'.
Now the problem is that my partial view (CreatePost.ascx) accepts a System.Web.Mvc.ViewUserControl<MessageBoard.Models.Post> and I'm not sure how to pass that from all my views above.
I'm also unsure know how to submit the .ascx values to a certain URL (i.e. /Topics/1/CreatePost), how do I tell the submit button to post to that URL?
Thanks in advance,
Marko
Ciao Marko,
Now the problem is that my partial
view (CreatePost.ascx) accepts a
System.Web.Mvc.ViewUserControl
and I'm not sure how to pass that from
all my views above.
I am not sure I understand what do you mean by "how to pass that from all my views above" but I am sure that you dont have to pass an instance of Post from your views. What is going on is that from your views you will invoke a controller action that creates the Post model object and then bind it to the CreatePost.ascx partial.
I'm also unsure know how to submit the
.ascx values to a certain URL (i.e.
/Topics/1/CreatePost), how do I tell
the submit button to post to that URL?
You have two options:
Inside your CreatePost.ascx partial you are probably using a form.
<% using (Html.BeginForm("action", "controller", FormMethod.Post, new {} )) { %>
If you use in the way I am showing you can change the first and the second params respectively to the names of the Action and the Controller that would habndle your submit.
The second option is using jQuery. Simply set an ID for your form and then
$("#myForm").submit(function(event) {
//post an ajax request to the server
});
Hope this helps!
P.S. To be able to reuse your CreatePost.ascx partial place it inside the shared view folder (where your master page is).
In regards to reusing a partial view which is not in the same view folder, use the following and pass in the model required, alternatively you can define a custom route for it.
<% html.RenderPartial("~/Views/<ControllerName>/<PartialViewName>.ascx", <model>);
#Marko
Another way would be to have in the PostController an Action like the following:
[HttpGet]
public ActionResult CreatePost( int topicId ) {
PostModel pm = _manager.CreateDefaultPost();
pm.TopicID = id;
return PartialView( "CreatePost", pm );
}
Then wherever you want to create a Post you can simply call this action that returns the strong-typed view for your new post.
Even if you have a supplementary http call over the network IMO this solution has the advantage to keep the initialization code of a new Post centralized to a single place.
From the View "point-of-view" the call to the action can be done when the user press the "New Post" button and then inject the received markup inside a modal dialog or in a place of your pleasure in the current page.
Hope it helps!