How to setup gridview in mvc
I would like to setup gridview under MyTickets tab.
How can I set this view to have only tickets from username eg 'testuser' ?
In controller I have below code. Table Zgloszenia is my table where I storing all information about tickets (date,username, id etc)
public ActionResult MyTickets(Zgloszenia model)
{
if (Session["UserID"] != null)
{
test dg = new test();
var item = dg.Zgloszenia.Where(x => x.UsrUsera == model.UsrUsera).SingleOrDefault();
return View(item);
}
else
{
return RedirectToAction("Login");
}
}
In view I have this code:
@model IEnumerable<Webform.Models.Zgloszenia>
@{
ViewBag.Title = "MyTickets";
WebGrid grid = new WebGrid(Model);
}
<h2>MyTickets</h2>
@if (Session["UserID"] != null)
{
<div>
Welcome: <a href="#">@Session["Username"]</a><br />
</div>
}
@grid.GetHtml(columns: new {
grid.Column("Opis"),
grid.Column("Priorytet"),
grid.Column("Srodowisko"),
grid.Column("NumerTaska"),
grid.Column("test"),
grid.Column("Date")
})
When I log in to my app and click Tab "MyTicket" I'm receiving below error:
A data source must be bound before this operation can be performed.
How I can fix this issue and set up view properly ?
c# asp.net-mvc model-view-controller
add a comment |
I would like to setup gridview under MyTickets tab.
How can I set this view to have only tickets from username eg 'testuser' ?
In controller I have below code. Table Zgloszenia is my table where I storing all information about tickets (date,username, id etc)
public ActionResult MyTickets(Zgloszenia model)
{
if (Session["UserID"] != null)
{
test dg = new test();
var item = dg.Zgloszenia.Where(x => x.UsrUsera == model.UsrUsera).SingleOrDefault();
return View(item);
}
else
{
return RedirectToAction("Login");
}
}
In view I have this code:
@model IEnumerable<Webform.Models.Zgloszenia>
@{
ViewBag.Title = "MyTickets";
WebGrid grid = new WebGrid(Model);
}
<h2>MyTickets</h2>
@if (Session["UserID"] != null)
{
<div>
Welcome: <a href="#">@Session["Username"]</a><br />
</div>
}
@grid.GetHtml(columns: new {
grid.Column("Opis"),
grid.Column("Priorytet"),
grid.Column("Srodowisko"),
grid.Column("NumerTaska"),
grid.Column("test"),
grid.Column("Date")
})
When I log in to my app and click Tab "MyTicket" I'm receiving below error:
A data source must be bound before this operation can be performed.
How I can fix this issue and set up view properly ?
c# asp.net-mvc model-view-controller
Why not just render a<table>directly? I don't see the value in usingWebGridhere.
– Dai
Nov 21 '18 at 16:02
@Dai, this might not be an actual code, it could be a minimal example to highlight the problem OP is having.
– Andrei
Nov 21 '18 at 16:11
add a comment |
I would like to setup gridview under MyTickets tab.
How can I set this view to have only tickets from username eg 'testuser' ?
In controller I have below code. Table Zgloszenia is my table where I storing all information about tickets (date,username, id etc)
public ActionResult MyTickets(Zgloszenia model)
{
if (Session["UserID"] != null)
{
test dg = new test();
var item = dg.Zgloszenia.Where(x => x.UsrUsera == model.UsrUsera).SingleOrDefault();
return View(item);
}
else
{
return RedirectToAction("Login");
}
}
In view I have this code:
@model IEnumerable<Webform.Models.Zgloszenia>
@{
ViewBag.Title = "MyTickets";
WebGrid grid = new WebGrid(Model);
}
<h2>MyTickets</h2>
@if (Session["UserID"] != null)
{
<div>
Welcome: <a href="#">@Session["Username"]</a><br />
</div>
}
@grid.GetHtml(columns: new {
grid.Column("Opis"),
grid.Column("Priorytet"),
grid.Column("Srodowisko"),
grid.Column("NumerTaska"),
grid.Column("test"),
grid.Column("Date")
})
When I log in to my app and click Tab "MyTicket" I'm receiving below error:
A data source must be bound before this operation can be performed.
How I can fix this issue and set up view properly ?
c# asp.net-mvc model-view-controller
I would like to setup gridview under MyTickets tab.
How can I set this view to have only tickets from username eg 'testuser' ?
In controller I have below code. Table Zgloszenia is my table where I storing all information about tickets (date,username, id etc)
public ActionResult MyTickets(Zgloszenia model)
{
if (Session["UserID"] != null)
{
test dg = new test();
var item = dg.Zgloszenia.Where(x => x.UsrUsera == model.UsrUsera).SingleOrDefault();
return View(item);
}
else
{
return RedirectToAction("Login");
}
}
In view I have this code:
@model IEnumerable<Webform.Models.Zgloszenia>
@{
ViewBag.Title = "MyTickets";
WebGrid grid = new WebGrid(Model);
}
<h2>MyTickets</h2>
@if (Session["UserID"] != null)
{
<div>
Welcome: <a href="#">@Session["Username"]</a><br />
</div>
}
@grid.GetHtml(columns: new {
grid.Column("Opis"),
grid.Column("Priorytet"),
grid.Column("Srodowisko"),
grid.Column("NumerTaska"),
grid.Column("test"),
grid.Column("Date")
})
When I log in to my app and click Tab "MyTicket" I'm receiving below error:
A data source must be bound before this operation can be performed.
How I can fix this issue and set up view properly ?
c# asp.net-mvc model-view-controller
c# asp.net-mvc model-view-controller
edited Nov 21 '18 at 16:12
Andrei
47.2k76992
47.2k76992
asked Nov 21 '18 at 16:00
TomaszTomasz
3714
3714
Why not just render a<table>directly? I don't see the value in usingWebGridhere.
– Dai
Nov 21 '18 at 16:02
@Dai, this might not be an actual code, it could be a minimal example to highlight the problem OP is having.
– Andrei
Nov 21 '18 at 16:11
add a comment |
Why not just render a<table>directly? I don't see the value in usingWebGridhere.
– Dai
Nov 21 '18 at 16:02
@Dai, this might not be an actual code, it could be a minimal example to highlight the problem OP is having.
– Andrei
Nov 21 '18 at 16:11
Why not just render a
<table> directly? I don't see the value in using WebGrid here.– Dai
Nov 21 '18 at 16:02
Why not just render a
<table> directly? I don't see the value in using WebGrid here.– Dai
Nov 21 '18 at 16:02
@Dai, this might not be an actual code, it could be a minimal example to highlight the problem OP is having.
– Andrei
Nov 21 '18 at 16:11
@Dai, this might not be an actual code, it could be a minimal example to highlight the problem OP is having.
– Andrei
Nov 21 '18 at 16:11
add a comment |
2 Answers
2
active
oldest
votes
In your action you are selecting a single item, not a collection to enumerate. On the contrary, the WebGrid expects a collection as a data source, so the way you declared things on the view is fine.
To check if that is indeed the issue, simply remove SingleOrDefault call in your action. If your Where call returns at least one record, you should be able to see it on the page:
test dg = new test();
var items = dg.Zgloszenia.Where(x => x.UsrUsera == model.UsrUsera).ToList();
return View(items);
Hi Andrei, Ok it's working but how can I do that to have grid with all tickets from username eg 'testuser' ? You know, user has logged to app via thier username and he would like to show only their tickets
– Tomasz
Nov 21 '18 at 16:25
@Tomasz, you are doing pretty much that, you are very close. Search online for how to identify current logged in user
– Andrei
Nov 21 '18 at 16:27
Yes, I've searched but still I have only empty tables :( Could You help me or give me a hint how to do this ? Should I something to do in controller or view ?
– Tomasz
Nov 21 '18 at 18:16
add a comment |
Are you working with Visual studio? If yes, you have to make a dataset. (local or online) You do not have a database at the moment so he saves it nowhere.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415979%2fhow-to-setup-gridview-in-mvc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your action you are selecting a single item, not a collection to enumerate. On the contrary, the WebGrid expects a collection as a data source, so the way you declared things on the view is fine.
To check if that is indeed the issue, simply remove SingleOrDefault call in your action. If your Where call returns at least one record, you should be able to see it on the page:
test dg = new test();
var items = dg.Zgloszenia.Where(x => x.UsrUsera == model.UsrUsera).ToList();
return View(items);
Hi Andrei, Ok it's working but how can I do that to have grid with all tickets from username eg 'testuser' ? You know, user has logged to app via thier username and he would like to show only their tickets
– Tomasz
Nov 21 '18 at 16:25
@Tomasz, you are doing pretty much that, you are very close. Search online for how to identify current logged in user
– Andrei
Nov 21 '18 at 16:27
Yes, I've searched but still I have only empty tables :( Could You help me or give me a hint how to do this ? Should I something to do in controller or view ?
– Tomasz
Nov 21 '18 at 18:16
add a comment |
In your action you are selecting a single item, not a collection to enumerate. On the contrary, the WebGrid expects a collection as a data source, so the way you declared things on the view is fine.
To check if that is indeed the issue, simply remove SingleOrDefault call in your action. If your Where call returns at least one record, you should be able to see it on the page:
test dg = new test();
var items = dg.Zgloszenia.Where(x => x.UsrUsera == model.UsrUsera).ToList();
return View(items);
Hi Andrei, Ok it's working but how can I do that to have grid with all tickets from username eg 'testuser' ? You know, user has logged to app via thier username and he would like to show only their tickets
– Tomasz
Nov 21 '18 at 16:25
@Tomasz, you are doing pretty much that, you are very close. Search online for how to identify current logged in user
– Andrei
Nov 21 '18 at 16:27
Yes, I've searched but still I have only empty tables :( Could You help me or give me a hint how to do this ? Should I something to do in controller or view ?
– Tomasz
Nov 21 '18 at 18:16
add a comment |
In your action you are selecting a single item, not a collection to enumerate. On the contrary, the WebGrid expects a collection as a data source, so the way you declared things on the view is fine.
To check if that is indeed the issue, simply remove SingleOrDefault call in your action. If your Where call returns at least one record, you should be able to see it on the page:
test dg = new test();
var items = dg.Zgloszenia.Where(x => x.UsrUsera == model.UsrUsera).ToList();
return View(items);
In your action you are selecting a single item, not a collection to enumerate. On the contrary, the WebGrid expects a collection as a data source, so the way you declared things on the view is fine.
To check if that is indeed the issue, simply remove SingleOrDefault call in your action. If your Where call returns at least one record, you should be able to see it on the page:
test dg = new test();
var items = dg.Zgloszenia.Where(x => x.UsrUsera == model.UsrUsera).ToList();
return View(items);
answered Nov 21 '18 at 16:09
AndreiAndrei
47.2k76992
47.2k76992
Hi Andrei, Ok it's working but how can I do that to have grid with all tickets from username eg 'testuser' ? You know, user has logged to app via thier username and he would like to show only their tickets
– Tomasz
Nov 21 '18 at 16:25
@Tomasz, you are doing pretty much that, you are very close. Search online for how to identify current logged in user
– Andrei
Nov 21 '18 at 16:27
Yes, I've searched but still I have only empty tables :( Could You help me or give me a hint how to do this ? Should I something to do in controller or view ?
– Tomasz
Nov 21 '18 at 18:16
add a comment |
Hi Andrei, Ok it's working but how can I do that to have grid with all tickets from username eg 'testuser' ? You know, user has logged to app via thier username and he would like to show only their tickets
– Tomasz
Nov 21 '18 at 16:25
@Tomasz, you are doing pretty much that, you are very close. Search online for how to identify current logged in user
– Andrei
Nov 21 '18 at 16:27
Yes, I've searched but still I have only empty tables :( Could You help me or give me a hint how to do this ? Should I something to do in controller or view ?
– Tomasz
Nov 21 '18 at 18:16
Hi Andrei, Ok it's working but how can I do that to have grid with all tickets from username eg 'testuser' ? You know, user has logged to app via thier username and he would like to show only their tickets
– Tomasz
Nov 21 '18 at 16:25
Hi Andrei, Ok it's working but how can I do that to have grid with all tickets from username eg 'testuser' ? You know, user has logged to app via thier username and he would like to show only their tickets
– Tomasz
Nov 21 '18 at 16:25
@Tomasz, you are doing pretty much that, you are very close. Search online for how to identify current logged in user
– Andrei
Nov 21 '18 at 16:27
@Tomasz, you are doing pretty much that, you are very close. Search online for how to identify current logged in user
– Andrei
Nov 21 '18 at 16:27
Yes, I've searched but still I have only empty tables :( Could You help me or give me a hint how to do this ? Should I something to do in controller or view ?
– Tomasz
Nov 21 '18 at 18:16
Yes, I've searched but still I have only empty tables :( Could You help me or give me a hint how to do this ? Should I something to do in controller or view ?
– Tomasz
Nov 21 '18 at 18:16
add a comment |
Are you working with Visual studio? If yes, you have to make a dataset. (local or online) You do not have a database at the moment so he saves it nowhere.
add a comment |
Are you working with Visual studio? If yes, you have to make a dataset. (local or online) You do not have a database at the moment so he saves it nowhere.
add a comment |
Are you working with Visual studio? If yes, you have to make a dataset. (local or online) You do not have a database at the moment so he saves it nowhere.
Are you working with Visual studio? If yes, you have to make a dataset. (local or online) You do not have a database at the moment so he saves it nowhere.
answered Nov 21 '18 at 16:05
Aymeric SamynAymeric Samyn
12
12
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415979%2fhow-to-setup-gridview-in-mvc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why not just render a
<table>directly? I don't see the value in usingWebGridhere.– Dai
Nov 21 '18 at 16:02
@Dai, this might not be an actual code, it could be a minimal example to highlight the problem OP is having.
– Andrei
Nov 21 '18 at 16:11