Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 表单提交成功后如何显示成功消息_C#_Asp.net Mvc - Fatal编程技术网

C# 表单提交成功后如何显示成功消息

C# 表单提交成功后如何显示成功消息,c#,asp.net-mvc,C#,Asp.net Mvc,我正在学习ASP.NETMVC。提交表单后,我想在主视图的submit按钮下显示一条带有部分视图的成功消息 <form action="myactionLInk" method="post"> <input type="text" name="Name" placeholder="Enter name here..."/> <input type="submit" value="Submit" /> </form> // He

我正在学习ASP.NETMVC。提交表单后,我想在主视图的submit按钮下显示一条带有部分视图的成功消息

<form action="myactionLInk" method="post">
     <input type="text" name="Name" placeholder="Enter name here..."/>
     <input type="submit" value="Submit" />
</form>

// Here would be some code to load partialview
<div>
   @Html.Partial("_userpartial")
</div>
下面是
\u userPartial.cshtml

<p> Data of @Viewbag.Name successfully saved.</p>
@Viewbag.Name的数据已成功保存


我希望部分视图在成功创建时加载到submit按钮下。我仍在学习MVC,因此非常感谢您的帮助。

为什么您需要局部视图来显示此成功消息?您可以简单地执行以下操作:

public class User
{
    [Key]
    public int UserId {get; set;}

    [Required]
    public int UserName {get; set;}
}
正如您所说,您是ASP.NET MVC的新手,因此我在这里为您提供一个完整的解决方案,说明如何在创建实体后显示成功消息。

首先编写一个模型类,如下所示:

public class User
{
    [Key]
    public int UserId {get; set;}

    [Required]
    public int UserName {get; set;}
}
然后在
用户
控制器中:

public class UserController: Controller
{
    [HttpGet]
    public ActionResult CreateUser()
    {
         return View();
    }

    [HttpPost]
    public ActionResult CreateUser(User user)
    {
        if(ModelState.IsValid)
        {
          // save the user here

          ViewBag.SuccessMessage = user.UserName + " has been created successfully!"
          ModelState.Clear();
          return View();
        }
        return View(user);
   }
}
然后在
CreateUser.cshmtl
视图中:

@using User

@using (Html.BeginForm("CreateUser", "User", FormMethod.Post))
{
    @Html.EditorFor(m => m.UserName)

   <input type="submit" value="Submit" />
}

@{
  if(ViewBag.SuccessMessage != null)
  {
     <div>
         @ViewBag.SuccessMessage
     </div>
  }
}
@使用用户
@使用(Html.BeginForm(“CreateUser”、“User”、FormMethod.Post))
{
@EditorFor(m=>m.UserName)
}
@{
if(ViewBag.SuccessMessage!=null)
{
@ViewBag.SuccessMessage
}
}

希望它能对您有所帮助。

我使用的是局部视图,因为成功消息包含此人的姓名,我可能会在以后添加更多详细信息。我已更新了答案。检查此行
ViewBag.SuccessMessage=user.UserName+“已成功创建!”