Click here to Skip to main content
15,886,037 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I run the above code it shows following error as follows

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /User1/Details

I run the application correctly

localhost:5129/User1/Details


What is the mistake in my above code?

What I have tried:

My models code as follows
C#
public class UserDetails1
{
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string Education { get; set; }
    public string Location { get; set; }
    public List<userdetails1> usersinfo { get; set; }
}

My controller code as follows (Controller Name as User1)
C#
[HttpPost]
public ActionResult Details(UserDetails1 user)
{
    UserDetails1 objuser = new UserDetails1();
    using (SqlConnection con = new SqlConnection("Data Source=Trenmax;Integrated Security=true;Initial Catalog=Sample"))
    {
        using (SqlCommand cmd = new SqlCommand("usercrudoperations", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@name", user.UserName);
            cmd.Parameters.AddWithValue("@education", user.Education);
            cmd.Parameters.AddWithValue("@location", user.Location);
            cmd.Parameters.AddWithValue("@status", "INSERT");
            con.Open();
            ViewData["result"] = cmd.ExecuteNonQuery();
            con.Close();
        }
    }
    return View();
}


My views code as follows (View name as Details)
@using (Html.BeginForm("Details","User1",FormMethod.Post)) {
    <table>
    <tbody>
    <tr>
        <td>User Name:</td>
        <td>@Html.TextBoxFor(u=>u.UserName)</td>
    </tr>
    <tr>
        <td>Education:</td>
        <td>@Html.TextBoxFor(u => u.Education)</td>
    </tr>
    <tr>
        <td>Location:</td>
        <td>@Html.TextBoxFor(u=>u.Location)</td>
    </tr>
    <tr>
        <td></td>
        <td>  </td>
    </tr>
    </tbody>
    </table>
}
<h4>User Details</h4>
@if (Model != null)
{
    if (Model.usersinfo.Count > 0)
    {
        @foreach (var item in Model.usersinfo)
        {
        }
        <table>
        <tbody>
        <tr>
            <th>UserId</th>
            <th>UserName</th>
            <th>Education</th>
            <th>Location</th>
        </tr>
        <tr>
            <td>@Html.DisplayFor(modelitem => item.UserId) </td>
            <td>@Html.DisplayFor(modelitem => item.UserName)</td>
            <td>@Html.DisplayFor(modelitem => item.Education)</td>
            <td>@Html.DisplayFor(modelitem => item.Location)</td>
        </tr>
        </tbody>
        </table>
    }
else {
    <b>No Details Found.</b>
}
}

<script>
$(function () {
    var msg = '@ViewData["result"]';
    if (msg == '1')
    {
        alert("User Details Inserted Successfully");
        window.location.href = "@Url.Action("InsertUserDetails", "User")";
    }
});
</script>
Posted
Updated 23-Feb-18 2:51am
v2
Comments
Richard Deeming 23-Feb-18 8:43am    
You don't seem to have an action called Details which accepts a GET request.
[HttpGet]
public ActionResult Details()
{
    return View();
}
benjaminemanuel13 26-Feb-18 10:27am    
Another problem you will have: your foreach is in the wrong place, they need to open and close around the html code that contains references to item. In fact, the loop should be after the first
<tr>...</tr>
and should close before the
</tbody>
that way, the generated html will be made up of the rows.

As in:

@foreach (var item in Model.usersinfo){<tr>...</tr>}

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