Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.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# 如何使用MVC4动态Html输入_C#_Ajax_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 如何使用MVC4动态Html输入

C# 如何使用MVC4动态Html输入,c#,ajax,asp.net-mvc,asp.net-mvc-4,C#,Ajax,Asp.net Mvc,Asp.net Mvc 4,我是MVC新手,使用MVC4。我有一系列用AJAX调用填充的级联下拉列表。Customer>Family>Assembly-该程序集决定了我需要用户填写多少Balun(具有属性的对象),Balun对象的数量取决于该程序集的要求 我为每个项目创建了一个单独的对象,并为此视图创建了一个viewmodel。然后,我将listOfBaluns传递给一个局部视图,以循环并创建HTML,不管需要多少 当我提交表单时,我总是只看到第一个Balun对象的属性。据我所知,我正确地使用了部分视图来循环浏览Balun

我是MVC新手,使用MVC4。我有一系列用AJAX调用填充的级联下拉列表。Customer>Family>Assembly-该程序集决定了我需要用户填写多少Balun(具有属性的对象),Balun对象的数量取决于该程序集的要求

我为每个项目创建了一个单独的对象,并为此视图创建了一个viewmodel。然后,我将listOfBaluns传递给一个局部视图,以循环并创建HTML,不管需要多少

当我提交表单时,我总是只看到第一个Balun对象的属性。据我所知,我正确地使用了部分视图来循环浏览Balun对象列表,但这些对象不会出现在FormCollection或Request.Form的帖子中

在我包含的代码中,我只是测试是否可以让2个balun工作,这应该可以解释为什么我要在视图模型的列表中显式添加2个balun记录

任何帮助都将不胜感激

以下是视图-

@model Nortech_Intranet.ViewModels.BalunTuningEntryViewModel
@using Nortech_Intranet.Models.BalunModels

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

    <div class="whiteBodyContainer">
        @*Text Entry fields*@
        <table style="margin: auto;">
            <tr>
                <th>
                    @Html.LabelFor(model => model.balunTuningRecord.Start_Timestamp)
                </th>
                <td>
                    @Html.TextBoxFor(model => model.balunTuningRecord.Start_Timestamp, new { @Value = @System.DateTime.Now } )
                </td>
                <th>
                    @Html.LabelFor(model => model.balunTuningRecord.Work_Order)
                </th>
                <td>
                    @Html.TextBoxFor(model => model.balunTuningRecord.Work_Order)
                </td>
                <th>
                    @Html.LabelFor(model => model.balunTuningRecord.Serial_Number)
                </th>
                <td>
                    @Html.TextBoxFor(model => model.balunTuningRecord.Serial_Number)
                </td>
            </tr>
        </table>
        <hr />    

        <table style="margin: auto;">
            <tr>
                @Html.Partial("_RenderBalun", Model.balunList)
            </tr>
        </table>
    </div>

    <p>
        <input type="submit" value="Create Tuning Record" />
    </p>
}
@model Nortech\u Intranet.ViewModels.baluntunigentryviewmodel
@使用Nortech_Intranet.Models.BalunModels
@使用(Html.BeginForm()){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@*文本输入字段*@
@LabelFor(model=>model.balunTuningRecord.Start\u时间戳)
@Html.TextBoxFor(model=>model.balunTuningRecord.Start_Timestamp,新的{@Value=@System.DateTime.Now})
@LabelFor(model=>model.balunTuningRecord.Work\u Order)
@Html.TextBoxFor(model=>model.balunTuningRecord.Work\u订单)
@LabelFor(model=>model.balunTuningRecord.Serial\u编号)
@Html.TextBoxFor(model=>model.balunTuningRecord.Serial\u编号)

@Html.Partial(“_RenderBalun”,Model.balunList)

}
视图模型-

using Nortech_Intranet.Models.BalunModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Nortech_Intranet.ViewModels
{
    public class BalunTuningEntryViewModel
    {
        public List<Customer> customerList { get; set; }
        public List<Family> familyList { get; set; }
        public List<Assembly> assemblyList { get; set; }
        public List<NWAnalyzerModel> nwAnalyzerModelList { get; set; }
        public List<NWAnalyzerCAL_ID> nwAnalyzerCAL_IDList { get; set; }
        public List<TestUser> testUserList { get; set; }
        public List<BalunRecord> balunList { get; set; }

        public BalunTuningRecord balunTuningRecord { get; set; }

        public BalunTuningEntryViewModel() : base ()
        {
            balunList = new List<BalunRecord>();

            BalunRecord newRecord = new BalunRecord();
            balunList.Add(newRecord);
            BalunRecord newRecord2 = new BalunRecord();
            balunList.Add(newRecord2);
        }

        public BalunTuningEntryViewModel(int i)
        {
            balunList = new List<BalunRecord>();

            for (int counter = 0; counter < i; counter++)
            {
                BalunRecord newRecord = new BalunRecord();
                balunList.Add(newRecord);
            }
        }
    }
}
使用Nortech_Intranet.Models.BalunModels;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
命名空间Nortech_Intranet.ViewModels
{
公共类BaluntunigentryViewModel
{
公共列表客户列表{get;set;}
公共列表familyList{get;set;}
公共列表assemblyList{get;set;}
公共列表nwAnalyzerModelList{get;set;}
公共列表nwAnalyzerCAL_IDList{get;set;}
公共列表testUserList{get;set;}
公共列表balunList{get;set;}
公共BalunTuningRecord BalunTuningRecord{get;set;}
public baluntunigentryViewModel():base()
{
balunList=新列表();
BalunRecord newRecord=新的BalunRecord();
添加(新记录);
BalunRecord newRecord2=新的BalunRecord();
balunList.Add(新记录2);
}
公共BalunTuningEntryViewModel(int i)
{
balunList=新列表();
用于(int计数器=0;计数器
局部视图-

@model IEnumerable<Nortech_Intranet.Models.BalunModels.BalunRecord>
@using Nortech_Intranet.Models.BalunModels

@{
    int counter = 1;
    foreach(BalunRecord record in Model)
    {
    <td>
        <table id="@("tblBalun"+@counter)"  style="border: 2px solid gray; visibility: collapse;">
            <tr>
                <th colspan="2" style="text-align: center;"><label>Balun #@counter</label></th>
            </tr>
            <tr>
                <th>
                    @Html.LabelFor(model => record.Tuning_Cap_1)
                </th>
                <td>
                    @Html.TextBoxFor(model => record.Tuning_Cap_1, new { id = "ddlBalun" + @counter + "Cap" + "1" })
                </td>
            </tr>
            <tr>
                <th>
                    @Html.LabelFor(model => record.Tuning_Cap_2)
                </th>
                <td>
                    @Html.TextBoxFor(model => record.Tuning_Cap_2, new { id = "ddlBalun" + @counter + "Cap" + "2" })
                </td>
            </tr>
        </table>
    </td>       

        counter = counter + 1;
    }
}
@model IEnumerable
@使用Nortech_Intranet.Models.BalunModels
@{
int计数器=1;
foreach(模型中的BalunRecord记录)
{
巴伦计数器
@LabelFor(model=>record.Tuning\u Cap\u 1)
@TextBoxFor(model=>record.Tuning_Cap_1,new{id=“ddlBalun”+@counter+“Cap”+“1”})
@LabelFor(model=>record.Tuning\u Cap\u 2)
@TextBoxFor(model=>record.Tuning_Cap_2,new{id=“ddlBalun”+@counter+“Cap”+“2”})
计数器=计数器+1;
}
}

我想出来了,解决方案是循环使用模型的索引。下面是视图中解决问题的新部分-

@{
    var counter = 1;
    for (int i = 0; i < Model.balunList.Count; i++)
    {
    <td>
    <table id="@("tblBalun" + @counter)"  style="border: 2px solid gray; visibility: collapse;">
        <tr>
            <th colspan="2" style="text-align: center;"><label>Balun #@counter</label></th>
        </tr>
        <tr>
            <th>
                @Html.LabelFor(model => model.balunList[i].Tuning_Cap_1)
            </th>
            <td>
                @Html.TextBoxFor(model => model.balunList[i].Tuning_Cap_1, new { id = "ddlBalun" + @counter + "Cap" + "1" })
            </td>
        </tr>
        <tr>
            <th>
                @Html.LabelFor(model => model.balunList[i].Tuning_Cap_2)
            </th>
            <td>
                @Html.TextBoxFor(model => model.balunList[i].Tuning_Cap_2, new { id = "ddlBalun" + @counter + "Cap" + "2" })
            </td>
        </tr>
    </table>
    </td>
        counter++;
    }   
}
@{
var计数器=1;
对于(int i=0;imodel.balunList[i].Tuning\u Cap\u 1)
@Html.TextBoxFor(model=>model.balunList[i].Tuning_Cap_1,new{id=“ddlBalun”+@counter+“Cap”+“1”})
@Html.LabelFor(model=>model.balunList[i].Tuning\u Cap\u 2)
@Html.TextBoxFor(model=>model.balunList[i].Tuning_Cap_2,new{id=“ddlBalun”+@counter+“Cap”+“2”})
计数器++;
}   
}

为什么不简化代码,只显示理解问题所需的最低限度?我认为这个问题并不难解决,但是没有人会去检查这么多代码。至少是我。买方法:太多的测试,太多的代码,但你的问题根本不清楚。大量的代码和。。。控制器的动作在哪里?我试图尽可能详细,但很好的建议是,我缩短了代码,所以现在它的所有脂肪都被删掉了。谢谢你的建议。