简体   繁体   English

如何将链接项列表作为一个数据传递给.net MVC中的控制器?

[英]How to pass a list of linked items as one data to controller in .net mvc?

I want to pass the text values in the div('111'...'555') along with the values in the input to the controller. 我想将div('111'...'555')中的文本值与输入中的值一起传递给控制器​​。 I was thinking of wrapping it up in a Json. 我当时正在考虑将其包装在Json中。 But don't know how to implement it properly. 但是不知道如何正确实施它。 Anyone has any advise? 有人有什么建议吗?

here is the code, 这是代码,

<div name="question" class="panel-body">
        111
        <label class="options">
            <span class="check-box"></span>
            <input type="radio" id="rating-1" name="rating" value="@model.value" />
        </label>
    </div>
    <div name="question" class="panel-body">
        222
        <label class="options">
            <span class="check-box"></span>
            <input type="radio" id="rating-2" name="rating" value="@model.value" />
        </label>
    </div>
    <div name="question" class="panel-body">
        333
        <label class="options">
            <span class="check-box"></span>
            <input type="radio" id="rating-3" name="rating" value="@model.value" />
        </label>
    </div>
    <div name="question" class="panel-body">
        444
        <label class="options">
            <span class="check-box"></span>
            <input type="radio" id="rating-4" name="rating" value="@model.value" />
        </label>
    </div>

Thanks! 谢谢!

Put div values in a span class="div-value". 将div值放在span class =“ div-value”中。 So that you can easily find this in jquery. 这样您就可以在jquery中轻松找到它。 For submitting to controller I have added here a button id ="submit" 为了提交给控制器,我在这里添加了一个按钮id =“ submit”

  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  <div name="question" class="panel-body">
       <span class="div-value"> 111 </span>
        <label class="options">
            <span class="check-box"></span>
            <input type="radio" id="rating-1" name="rating" value="@model.value" />
        </label>
    </div>
    <div name="question" class="panel-body">
       <span class="div-value"> 222 </span>
        <label class="options">
            <span class="check-box"></span>
            <input type="radio" id="rating-2" name="rating" value="@model.value" />
        </label>
    </div>
    <div name="question" class="panel-body">
       <span class="div-value"> 333 </span>
        <label class="options">
            <span class="check-box"></span>
            <input type="radio" id="rating-3" name="rating" value="@model.value" />
        </label>
    </div>
    <div name="question" class="panel-body">
      <span class="div-value">  444 </span>
        <label class="options">
            <span class="check-box"></span>
            <input type="radio" id="rating-4" name="rating" value="@model.value" />
        </label>
    </div>
  <button id="submit"> Action</button> 

<script>
$("#submit").on("click", function(){ 
   var singleObject = {}, listObject = [];
   $("div[name='question']").each(function(){
    singleObject = {};
    singleObject.divvalue = $("span[class='div-value']", this).text().trim();
    singleObject.radiovalue = $("input[type='radio']", this).val().trim();
    listObject.push(singleObject);

   });
    var jsonData = {};
    jsonData.ExamModel = listObject;
    var data = "{'model':" + JSON.stringify(jsonData) + "}";
 $.ajax({
          type: 'POST',
          contentType: "application/json; charset=utf-8",
          url: '/exam/save',
          dataType: 'json',
          data: data,
          success: function (data) {
              alert(data.Message);   
            },
            error: function (e) {
                 alert("Error Occurred")
            } 
       });
     });
 </script> 

Make View Model like this 这样制作视图模型

 public class ExamModel
       {
           public decimal divvalue { get; set; }
           public decimal radiovalue { get; set; }
       }
 public class ExamModelList
       {
           public List<ExamModel> ExamModel{ get; set; }

       }

And in Action do like this 在行动中做到这一点

public JsonResult Save(ExamModelList model)
    { 
        // Process Model Here 
        return Json(new {   Message = "response Message : Alhamdulillah worked "});
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将项目列表从$ .get()传递到MVC控制器 - Pass list of items from $.get() to MVC controller 如何将JavaScript数据传递到控制器ASP.NET MVC - How to pass javascript data to controller ASP.NET MVC 在MVC 5中,如何将表对象数据作为列表传递给Controller View Model? - In MVC 5 how to pass table Object data as a list to Controller View Model? 如何将数据从 MVC controller 传递到 javascript? - How to pass data from MVC controller to javascript? 如何在ASP.NET MVC中重复表单控件并将其作为列表传递给控制器 - How to repeat form controls in asp.net mvc and pass them as a list to controller 如何将列表从控制器传递到asp.net mvc中的javascript函数? - How to pass list from controller to javascript function in asp.net mvc? 如何将数据从URL传递到Web Api控制器以在ASP.NET MVC中进行测试 - how can pass data From URL to web Api controller for testing in asp.net mvc 如何从ASP.NET MVC中的控制器传递JSON中的chart.js图表​​数据对象 - How to pass a chart.js chart data object in json from a controller in asp.net mvc 将数据从ASp.NET MVC Controller传递到JavaScript函数 - Pass data from ASp.NET MVC Controller to JavaScript function 将数据从一个控制器动作传递到MVC中的另一个视图 - Pass data from one controller action to another view in mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM