【问题标题】:retrieve model properties values inside GET method in mvc在 mvc 的 GET 方法中检索模型属性值
【发布时间】:2016-04-11 11:38:46
【问题描述】:

我有以下GET 方法,这是创建表单的代码

 public ActionResult Add_Product(string Product_ID)
 { 
        AddNewProduct sample = new AddNewProduct();

        return View(sample);
 }

这是它的模型类

public class AddNewProduct
{
    public string Product_ID { get; set; }

    ...
}

这是create form

    @model project_name.Models.AddNewProduct    

    <h4>Add New Product</h4>    

    @using (Html.BeginForm()) 
    {
        @Html.AntiForgeryToken()

        <div class="form-horizontal">                
            @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group">
                @Html.LabelFor(model => model.Product_ID, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Product_ID, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Product_ID, "", new { @class = "text-danger" })
               </div>
         </div>

         .....

<div>
    @Html.ActionLink("Back to AddNewProduct", "AddNewProduct","Home" , new {Product_ID = Model.Product_ID})
</div>

    }

我可以使用此视图页面插入 Product_ID。但是一旦我单击此 Back to AddNewProduct 链接并调试 AddNewProduct 我看不到 string Product_ID 的任何值

为什么这个模型属性绑定不好

【问题讨论】:

  • 向我们展示您的AddNewProduct 操作代码。
  • @kez...现在查看我更新的答案。我认为通过在@Html.ActionLink 的参数末尾发送一个null,您的问题将得到解决。
  • 不完全确定您要做什么。您最初传递给视图的模型尚未设置Product_ID,因此您链接将null 再次传递回该方法。要设置该值,请使用AddNewProduct sample = new AddNewProduct{ Product_ID = Product_ID }; return View(sample);,但不清楚为什么您有一个与该属性关联的DropDownListFor()。是否要将选定的选项值传递回方法?
  • 我没有看到 DropDownListFor() 助手,
  • 对不起,我的意思是TextBoxFor()

标签: c# asp.net-mvc linq razor asp.net-mvc-5


【解决方案1】:

您需要分配价值。将您从 get 方法发送的 Product_ID 的值分配给类的 Product_ID 属性

public ActionResult Add_Product(string Product_ID)
     { 
            AddNewProduct sample = new AddNewProduct();
            sample.Product_ID = Product_ID;
            return View(sample);
     }

【讨论】:

  • 不,它仍然是 null 这里我只使用一种方法 Add_Product ,那就是 GET 方法
  • 点击锚标签的链接是否包含Product_Id。还要检查参数发送是否有拼写错误或差异。
  • 没有检查anchor tagany spelling mistakes,锚标签不包含任何值,始终为空,拼写没有错
  • 您在 HomeController 上有任何 AddNewProduct 获取方法吗?
  • 那么点击动作链接时会调用哪个方法?
【解决方案2】:

要将文本框的值传递给Add_Product() GET 方法,您需要使用javascript/jquery。将您的@Html.ActionLink(..) 替换为

<a href="#" class="back">Back to AddNewProduct</a>

并添加以下脚本

var baseUrl = '@Url.Action("Add_Product", "Home")';
$('#back').click(function() {
    var id = $('#Product_ID').val();
    location.href = baseUrl + '/' + id;
}}

注意:location.href = baseUrl + '/' + id; 假设你已经用{controller}/{action}/{Product_ID} 定义了一个特定的路由,否则它需要是

location.href = baseUrl + '?Product_ID=' + id;

或者,将方法参数更改为string id,使其使用默认路由

另请注意,您可能希望将方法更改为

public ActionResult Add_Product(string Product_ID)
{ 
    AddNewProduct sample = new AddNewProduct
    {
        Product_ID = Product_ID
    };
    return View(sample);
}

这样,如果您单击 Back to AddNewProduct 链接,视图将显示您之前输入的值。

【讨论】:

  • 对你的回答没有异议,但是为了知识,我也可以将这个答案应用于复杂的对象吗?
  • 当然(有一些限制)。您实际上想传递给该方法的是什么?
  • 我能传递 15 个包含 HTML 标签的对象值吗?我在猜测由于长网址,我将无法通过,我正确吗?
  • 可能不会——它可能超过查询字符串限制并抛出异常。它会创建一个非常丑陋的网址。但是您想要这样做的事实表明您的设计存在问题。
【解决方案3】:

@Html.ActionLink 的第二个参数是actionName,但您发送了型号名称 (AddNewProduct)。改成这样:

@Html.ActionLink("Back to AddNewProduct", "Add_Product","Home" , new {Product_ID = Model.Product_ID})

或者使用这个重载(使用这个ActionLink重载时也需要发送null):

@Html.ActionLink("Back to AddNewProduct", "Add_Product","Home" , new {Product_ID = Model.Product_ID}, null)

【讨论】:

  • 是的,在这里你是对的,但是一旦我按照你提到的那样进行更改,我就可以直接使用该方法,但 Product_ID 的值为空
  • 仍然是 null ,这是因为页面刷新,GET 方法刷新它的模型?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-15
  • 1970-01-01
  • 2012-09-09
相关资源
最近更新 更多