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
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)
[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>