Click here to Skip to main content
15,867,986 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I have a table in database with some columns are: [id], [title], [data]

I have created a page to allow user posting new thread. Example: If I post a new thread with the title is: "Here is my title". After submitting, the title will be converted to a link
HTML
<a href="/NewThread/Here-is-my-title">Here is my title</a>

I have done that control.

Now, when I click on the link, it will load data from column [title] in database and put it in URL. Like this:
HTML
http://www.example.com/NewThread/Here-is-my-title

Can you tell me how to do that?

I'm using MVC 5. If I convert the title to an a tag, it will need a controller and an action to execute. As the link above, the controller name is "NewThread", and the action name is "Here-is-my-title".

But my problem is: "Here-is-my-title" is not an action, it's a data which was loaded from column [title] in database.

Please help me. Thank you.
Posted

1 solution

Define the function in .Layout.cshtml
JavaScript
<script>
$(document).ready(function () {
            $('[data-action=navigate]').on('click', function (e) {
                e.preventDefault();
                var url = $(this).attr('data-action-target');
                var params = $(this).attr('data-action-parameters');
                url = url + (params ? ("?" + params) : "");
                window.location.href = url;
            });
</script></script>


and call it in the Html link as;

JavaScript
<a href="#" data-action="navigate" data-action-target="@Url.Action("controller-method", "NewThread" new {title="Here-is-my-title="})">LinkName</a>


It will give you the url:
../NewThread/Here-is-my-title
and in the controller action can define
public ActionResult controller-method(string title) 
{
//you will get the title here
}
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900