【问题标题】:Model Binding not working ASP.NET模型绑定不起作用 ASP.NET
【发布时间】:2014-05-06 18:51:12
【问题描述】:

我有这个模型:

public class TaskCreation
{
    public task_description task_desc { get; set; }
    public List<Metric> metric { get; set; }
    public List<Context> context { get; set; }
    public int scenarioId { get; set; }
    public short meas_id { get; set; }

}
public class Metric
{
    public string name { get; set; }
    public string value { get; set; }
}
public class Context
{
    public string name { get; set; }
    public string values { get; set; }
    public string upper_bound { get; set; }
    public string lower_bound { get; set; }
}

这是我的控制器方法:

public ActionResult CreateTask2()
    {
        TaskCreation tc = new TaskCreation();
        tc = TempData["TCObject"] as TaskCreation;
        return View(tc);
    }
    [HttpPost]
    public ActionResult CreateTask2(TaskCreation TaskCreation)
    {
        //some code
    }

因此,视图接收一个 TaskCreation 对象,并且有字段可以为每个指标和值输入一个值,每个上下文的上限和下限,但在 [httppost] CreateTask2 中,指标和上下文对象为空! task_description 对象中的其他属性绑定正确!我该如何解决?

这是我的看法:

@model Project.Models.TaskCreation
@using (Html.BeginForm("CreateTask2", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">

    <hr />
    @Html.ValidationSummary(true)
 <div class="form-group">
        @Html.LabelFor(model => model.task_desc.Is_valid, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.task_desc.Is_valid)
            @Html.ValidationMessageFor(model => model.task_desc.Is_valid)
        </div>
    </div>
    <br />
    <div class="form-group">
        @Html.LabelFor(model => model.task_desc.repeat_interval, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.task_desc.repeat_interval)
            @Html.ValidationMessageFor(model => model.task_desc.repeat_interval)
        </div>
    </div>
    <div class="form-group">
        <p>Enter the metrics values:</p>
    </div>
    @foreach(var m in Model.metric)
    {
        <div class="form-group">
        @Html.Label(m.name, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(x => m.value, new { placeholder = "Value" })
            @Html.ValidationMessageFor(x => m.value)
        </div>
            <br />
    </div>
    }
    <div class="form-group">
        <p>Choose the constraints for each context:</p>
    </div>
        @foreach(var c in Model.context)
    {
            <div class="form-group">
                @Html.Label(c.name, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <table>
                        <tr>
                            <td>
                                @Html.TextBoxFor(con => c.lower_bound, new { placeholder = "Lower Bound" })
                                @Html.ValidationMessageFor(con => c.lower_bound)<br />
                                @Html.TextBoxFor(con => c.higher_bound, new { placeholder = "Upper Bound" })
                                @Html.ValidationMessageFor(con => c.higher_bound)
                            </td>
                            <td>OR</td>
                            <td>
                                @Html.TextBoxFor(con => c.values, new { placeholder = "Selected Values Separated by Spaces" })
                                @Html.ValidationMessageFor(con => c.values)
                            </td>
                            <br />
                        </tr>
                    </table>
                </div>
            </div>
    }
     <div class="form-group">
          <div class="col-md-offset-2 col-md-10">
              <input type="submit" class="btn btn-default" />
          </div>
     </div>
</div>
}

谢谢

【问题讨论】:

  • 你用我的回答解决了你的问题吗?
  • 很抱歉我离开了几天,我现在就试试!谢谢!

标签: asp.net asp.net-mvc binding model http-post


【解决方案1】:

用 For 循环代替 Foreach -

  @for( int i = 0; i < Model.metric.Count; i++)
    {
        <div class="form-group">
        @Html.Label(Model.metric[i].name, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(m => m.metric[i].value, new { placeholder = "Value" })
            @Html.ValidationMessageFor(m => m.metric[i].value)
        </div>
            <br />
    </div>
    }

同样,您也可以继续使用上下文属性。

更新:请查看以下示例,了解如何在父模型中获取子模型的属性。我们可以使用 FOR 循环来迭代子模型。如果我们使用 For 循环,我们可以在服务器端获取子模型的值,而无需任何额外的工作。检查以下示例 -

假设我们有以下模型 -

public class DataModel
{
    public string Name { get; set; }
    public string Email { get; set; }
    public List<XhrViewModel> eles { get; set; }
}

public class XhrViewModel
{
    public string data1 { get; set; }
    public string data2 { get; set; }
}

然后我们有以下控制器动作,它将返回创建视图 -

    public ActionResult CreateData()
    {
        DataModel m = new DataModel();
        m.eles = new List<XhrViewModel>();
        m.eles.Add(new XhrViewModel());
        m.eles.Add(new XhrViewModel());
        m.eles.Add(new XhrViewModel());
        return View(m);
    }

而且创建视图非常简单 -

@model Rami.Vemula.Dev.Mvc.Controllers.DataModel

@{
    ViewBag.Title = "CreateData";
}

<h2>CreateData</h2>

@using (Html.BeginForm("SubmitData", "Home", FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>DataModel</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Email, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email)
                @Html.ValidationMessageFor(model => model.Email)
            </div>
        </div>

        @for (int i = 0; i < Model.eles.Count; i++)
        {
            <div class="form-group">
                @Html.LabelFor(model => model.eles[i].data1, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.eles[i].data1)
                    @Html.ValidationMessageFor(model => model.eles[i].data1)
                </div>
            </div>
        }

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" id="ClickMe" class="btn btn-default" />
            </div>
        </div>
    </div>
}

如果签入上面的 View,我使用 FOR 循环而不是 Foreach 来渲染子 Model 属性,即 xhrViewModel。

点击提交按钮后,您将点击 SubmitData 控制器操作 -

    public ActionResult SubmitData(DataModel m)
    {
        return View();
    }

现在当我们运行应用程序时 -

然后在提交按钮上,您以这种方式获取 SubmitData 控制器操作中的值 -

【讨论】:

  • 再解释一下……现在回传的是几个键,例如“m.value”。模型绑定器并不真正知道如何将其绑定回模型。如果您使用 for 循环,每个循环都将类似于:Model.metric[1].value。所以它会在索引 1 处将它绑定回模型中。
  • 是的,我正在给你一个简单的代码
  • @ErikElkins 请检查我的更新答案。希望它能解决问题。
  • @caj,你觉得我的回答有帮助吗?
  • @ramiramilu 我尝试了 for 循环而不是 foreach,结果相同,在 httppost actionresult 中,指标为空。
猜你喜欢
  • 1970-01-01
  • 2018-06-18
  • 1970-01-01
  • 1970-01-01
  • 2021-03-07
  • 1970-01-01
  • 1970-01-01
  • 2012-05-31
  • 2021-11-13
相关资源
最近更新 更多