【问题标题】:Get value of an input c# mvc获取输入 c# mvc 的值
【发布时间】:2015-09-13 17:48:42
【问题描述】:

我有输入框,我需要从中获取值,以便稍后将它们重定向到特定控制器,但我不知道如何访问这些值。有人可以指导我如何获取这些值吗?

 <form action="@Url.Action("AddSpecific", "Comment", new { AuthorT ="1", WebSiteT = "1", postIdT = 1, TextT = "1",TitleT = "1" })" method="post">

<h1>
    <label for="Title">Title</label>
    <input name="Title" id="Title" type="text"  required />
</h1>
<h1>
    <label for="Text">Text</label>
    <input name="Text" id="Text" type="text" required />
</h1>
<h1>
    <label for="AuthorSite">AuthorSite</label>
    <input name="AuthorSite" id="AuthorSite" required />
</h1>
<h1>
    <label for="Author">Author</label>
    <input name="Author" id="Author" type="text" required />
</h1>
<h1>a
    <label for="IdOfPost">IdOfPost</label>
    <input name="IdOfPost" id="IdOfPost" type="number" required />
</h1>

<input type="submit" value="Post comment" />

【问题讨论】:

  • 您是要获取 javascript 还是 c# 中的值?
  • c# 实际上,如果你能在 java 脚本中找到解决方案,我会非常高兴
  • 语法和可读性。
  • 您需要访问 MVC 站点并完成基本教程。特别是,如何使用模型,如何使用 html 帮助器在视图中呈现该模型,以及如何定义控制器方法以便在提交表单时绑定模型。

标签: c# jquery html asp.net-mvc asp.net-mvc-3


【解决方案1】:

我会使用模型来传递并用作您操作的参数。

您需要更改表单 html,以免发送默认值。变化:

<form action="@Url.Action("AddSpecific", "Comment", new { AuthorT ="1", WebSiteT = "1", postIdT = 1, TextT = "1",TitleT = "1" })" method="post">

收件人:

<form action="@Url.Action("AddSpecific", "Comment")" method="post">

您的 MySpecificObject(您的操作的参数对象)。请注意,属性名称与您输入的名称属性相匹配,这很重要:

public class MySpecificObject
{
   public string AuthorSite {get;set;}
   public string Title {get;set;}
   public string Text {get;set;}
   public string Author {get;set;{
   public string IdOfPost {get;set;}
}

更新您的 CommentController:

public ActionResult AddSpecific(MySpecificObject mySpecificObject)
{
   //mySpecificObject.IdOfPost
   //do work
}

此外,如果您决定要通过 JavaScript 和 Ajax 执行此操作,请查看此 StackOverflow 帖子:Post form data to Controller's action with Ajax

【讨论】:

    【解决方案2】:

    当您使用声明的属性构建您的Form 时,您实质上表明了它应该尝试发布 的位置。一旦您概述了所述属性,提交按钮将执行到该位置。模型视图控制器会自动执行你的一些请求,所以你指向的控制器:

    public ActionResult Submit(int id, string name, string email)
    {
         // Utilize parameters.
    }
    

    因此,如果您使用与 Controller 中的参数匹配的 name 概述表单字段,MVC 将尝试将所述字段直接关联到 Controller。因此,您当前的方法甚至可能不需要发生。

    【讨论】:

    • 这是我的控制器 public ActionResult AddSpecific(String AuthorT,String WebSiteT, int postIdT ,String TextT,String TitleT ) { Comment C = new Comment { Author = AuthorT, AuthorSite = WebSiteT, IdOfPost = postIdT,文本 = TextT ,Title=TitleT,CommentOnPost=null}; db.Comment.Add(C); db.SaveChanges();返回视图(); }
    【解决方案3】:

    您的输入元素应与控制器上的参数具有相同的名称值

    例如,如果您的控制器如下所示:

    public ActionResult AddSpecific(String AuthorT)
    {
        //Do Some Actions
        return View();
    }
    

    您应该有一个包含名称为“AuthorT”的元素的表单:

    <form action="@Url.Action("AddSpecific")" method="post">
    
    <h1>
        <label for="Title">Title</label>
    </h1>
        <input name="AuthorT" id="AuthorT" type="text"  required />
        <input type="submit" value="Post Comment"/>
    </form>
    

    看到输入元素的name值与控制器参数同名,控制器会取这个值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-22
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      • 2019-07-06
      • 2021-11-22
      • 1970-01-01
      相关资源
      最近更新 更多