使用MVC将数据插入数据库
当我运行上面的代码时,它显示以下错误如下
'/'应用程序中的服务器错误。
无法找到资源。
描述:HTTP 404.您要查找的资源(或其中一个依赖项)可能已被删除,更改名称,或暂时不可用。请查看以下网址,确保拼写正确。
请求的URL:/ User1 / Details
我正确运行应用程序
localhost:5129 / User1 / Details
我上面的代码有什么错误?
我尝试过:
我的模型代码如下
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; }
}
我的控制器代码如下(控制器名称为User1)
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>
(function(){
var msg ='@ ViewData [结果]';
if(msg =='1')
{
alert(用户详细信息已成功插入);
window.location.href =@ Url .Action(InsertUserDetails,User);
}
});
< / script>
(function () { var msg = '@ViewData["result"]'; if (msg == '1') { alert("User Details Inserted Successfully"); window.location.href = "@Url.Action("InsertUserDetails", "User")"; } }); </script>