如何从Html.RadioButtonFor将列表传递给控制器​​?

时间:2015-05-22 16:11:25

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

所以基本上我有一个视图,它有一个创建新按钮,然后是一个项目列表,在我希望有一个单选按钮的项目列表中,允许用户选择它并将项目的详细信息转移到点击“新建”

时创建新页面

这就是我目前的看法:

 <tbody>
       @for (int i = 0; i < Model.newExpenseFOPList.Count; i++)
         {   
        <tr>         
          <td>@Html.TextBoxFor(m => m.newExpenseFOPList[@i].Category, new {style="width:100px; border-style: none;", @readonly = "readonly", onfocus="this.blur()", @maxlength = "60"})</td>
          <td>@Html.TextBoxFor(m => m.newExpenseFOPList[@i].SubCategory, new {style="width:150px; border-style: none;", @readonly = "readonly", onfocus="this.blur()", @maxlength = "60"})</td>
          <td>@Html.TextBoxFor(m => m.newExpenseFOPList[@i].CreditFund, new {style="width:45px; border-style: none;", @readonly = "readonly", onfocus="this.blur()",  @maxlength = "8"})
          @Html.TextBoxFor(m => m.newExpenseFOPList[@i].CreditORGN, new {style="width:45px; border-style: none;", @readonly = "readonly", onfocus="this.blur()",  @maxlength = "8"})
          @Html.TextBoxFor(m => m.newExpenseFOPList[@i].CreditProgram, new {style="width:45px; border-style: none;", @readonly = "readonly", onfocus="this.blur()",  @maxlength = "5"})</td>
          <td>@Html.TextBoxFor(m => m.newExpenseFOPList[@i].Amount, new { style="width:70px; border-style: none;",  @maxlength = "50", @onkeypress="return isNumber(event)"})</td>
          <td>@Html.RadioButtonFor(model => model.newExpenseFOPList[@i].CopyRadioButton, "CopyRadioButton",  new { @Name="group1", @Value="copy"}) </td>
        </tr>
         }                       
 </tbody>

这是与此观点相关的控制器:

[HttpPost]
        public ActionResult Expense(NewExpenseViewModel newExpenseViewModel, string submitAction, EditExpenseViewModel editExpenseViewModel)
        {
            //declare a variable of type list of ExpenseDistModel and fetch the SP getExpenseList values into it
            List<ExpenseDistModel> expenseModelList = DataAccess.getExpenseList();

            //instantiate the viewModel along with the values previously fetched by expenseModel
            editExpenseViewModel = new EditExpenseViewModel(expenseModelList);



            //Check Category is not already in the table or that the field is not empty 
            for (int i = 0; i < newExpenseViewModel.newExpenseFOPList.Count; i++)
            {
                if (newExpenseViewModel.selectedExpenseCategory == newExpenseViewModel.newExpenseFOPList[i].Category && newExpenseViewModel.selectedExpenseSubCategory == newExpenseViewModel.newExpenseFOPList[i].SubCategory || newExpenseViewModel.newExpenseFOPList[i].Category == null)
                {
                    ViewBag.Message1 = "Category already exists or the field is empty.";

                    //invoke SP to populate table with Expense info
                    List<ExpenseDistModel> newExpenseModelList = DataAccess.getExpenseList();

                    //invoke SP to populate Category Dropdown in Create New Expense
                    List<CategoryModel> newCategoryList = DataAccess.getCategory();

                    //invoke SP to populate SubCategory Dropdown in Create New Expense
                    // List<SubCategoryModel> newExpenseSubCategoryList = DataAccess.getSubCategory();

                    List<SubCategoryModel> newExpenseSubCategoryList = new List<SubCategoryModel>();

                    //instantiate the newExpenseViewModel along with the values previously fetched by expenseDistModel
                    newExpenseViewModel = new NewExpenseViewModel(newExpenseModelList, newCategoryList, newExpenseSubCategoryList);

                    //return the viewModel
                    return View("CreateNewExpense", newExpenseViewModel);
                }
            }
                return RedirectToAction("EditExpense", new
                {
                    newExpenseCategory = newExpenseViewModel.selectedExpenseCategory,

                    newExpenseSubCategory = newExpenseViewModel.selectedExpenseSubCategory,

                    expenseCreditFund = newExpenseViewModel.newExpenseCreditFund,

                    expenseCreditORGN = newExpenseViewModel.newExpenseCreditORGN,

                    expenseCreditProgram = newExpenseViewModel.newExpenseCreditProgram,

                    submitAction = submitAction,

                    modifiedby = "test"
                });
        }

当我将鼠标悬停在newExpenseViewModel上时,我会看到除了radiobutton之外的所有值,这是null - 任何想法?

更新:我通过将viewmodel而不是列表本身传递给radiobutton来解决我的问题 - 现在我需要将其初始化为“未选中”

 <td>@Html.RadioButtonFor(model => model.CopyRadioButton,@i, new{@checked = false})</td>

0 个答案:

没有答案