MVC Razor WebGrid Get Selected Row on Button Submi

2019-08-23 09:14发布

How can i get WebGrid Selected Row into Hidden Variable.

I am using the following code.

@{
var grid = new WebGrid(@ViewBag.ImageFiles, canPage: false, rowsPerPage: 5, selectionFieldName: "SelectedRow", ajaxUpdateContainerId: "gridImages");
}
<div id="gridContent">
@grid.GetHtml(tableStyle: "webGrid",
            htmlAttributes: new { id = "gridImages" },
            headerStyle: "header",
            displayHeader: true,
            alternatingRowStyle: "alt",
            selectedRowStyle: "select",
            columns: grid.Columns(
            grid.Column("Select", format: (item) => item.GetSelectLink("Select")),
            grid.Column("File", "Image")))
</div>
<input type="button" value="Submit" onclick="javascript getSelectedRow();"/>
@{
System.Dynamic.ExpandoObject expobject = null;
if (grid.HasSelection)
{
    expobject = (System.Dynamic.ExpandoObject)grid.SelectedRow.Value;
<input type="hidden" id="hdnSelectedRow" name="hdnSelectedRow" value="@(expobject == null ? "" : expobject.FirstOrDefault().Value.ToString())" />
}
}

Here i am binding Datatable to Webgrid from the following code

foreach (DataRow row in imageFiles.Rows)
        {
            var obj = (IDictionary<string, object>)new ExpandoObject();
            foreach (DataColumn col in imageFiles.Columns)
            {
                obj.Add(col.ColumnName, row[col.ColumnName]);
            }
            result.Add(obj);
        }
        ViewBag.ImageFiles = result;

When i click on submit button i am not getting hidden variable and value in javascript.

2条回答
啃猪蹄的小仙女
2楼-- · 2019-08-23 09:35

when a webgrid is rendered it is just an html table. I could be wrong but I don't think that your code will change with what is clicked. For changing a hidden value to match a selected row I always use jquery. Try something like this to see if it works for you.

<script type="text/javascript">
   $(document).ready(function(){
       $('#tableID').on('click', function(){
           $('#hdnSelectedRow').val($(this).closest('tr').find('#yourField').val());
       });
   });
</script>
查看更多
可以哭但决不认输i
3楼-- · 2019-08-23 09:47

I find the solution to get selected row.

Following is the code.

@{
var grid = new WebGrid(@ViewBag.ImageFiles, selectionFieldName: "SelectedRow");
}
<div id="gridImages">
@grid.GetHtml(tableStyle: "webGrid",
            headerStyle: "header",
            displayHeader: true,
            alternatingRowStyle: "alt",
            selectedRowStyle: "select",
            mode: WebGridPagerModes.All,
            columns: grid.Columns(
            grid.Column("Select", format: @<text>@Html.RadioButton("rbFile", "", false) </text>),
            grid.Column("File", "File", format: @<text>@item.File</text>)))
</div>

In the javascript on button click i am using the following code to get selected row

var selectedValue = "";
                        var isSelected = false;
                        $('#gridImages table tr').each(function () {
                            if ($(this).find("input[id*='rbFile']").length > 0) {
                                if ($(this).find("input[id*='rbFile']")[0].checked == true) {
                                    isSelected = true;
                                    selectedValue = $(this)[0].innerText;
                                }
                            }
                        });
                        if (isSelected == true) {
                            $(this).dialog("close");
                            txtBg = document.getElementById("txtBg");
                            txtBg.value = selectedValue;
                        }
                        else {
                            alert("Please select atleast one file.");
                        }
查看更多
登录 后发表回答