You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于MVC通过复选框提交GUID列表是否可行?Razor实现咨询

How to Submit Selected GUIDs via Checkboxes with Razor (No jQuery)

Got it, let's break this down step by step—you don't need jQuery at all for this, just plain Razor and HTML that plays nice with ASP.NET MVC's model binding. Here's exactly what you need to do:

First, Align on the Model Structure

Just to make sure we're on the same page, your existing models look like this (I'll restate for context):

public class MyModel
{
    public List<Guid> SelectedIds { get; set; }
    public List<Option> Options { get; set; }
}

public class Option
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

Step 1: Build the Razor View

The core trick here is getting the checkbox name attribute right—this is how MVC's model binder knows to collect selected values into your SelectedIds list. Here's the view code (assuming it's a strongly-typed view with @model MyModel):

@model MyModel

<form asp-action="Save" method="post">
    @foreach (var option in Model.Options)
    {
        <div class="checkbox-item">
            <!-- Critical: name must be "SelectedIds" to bind to the List<Guid> -->
            <input type="checkbox" name="SelectedIds" value="@option.Id.ToString()" />
            <label for="@option.Id">@option.Name</label>
        </div>
    }

    <button type="submit" class="btn btn-primary">Save Selections</button>
</form>

Alternative: Using Razor Helpers (Optional)

If you prefer using Razor's built-in helpers instead of raw HTML, this works too. Note that Html.CheckBox renders an extra hidden input (to handle unselected states), but it won't break the list binding:

@foreach (var option in Model.Options)
{
    <div>
        @Html.CheckBox("SelectedIds", false, new { value = option.Id.ToString(), id = option.Id })
        @Html.Label(option.Id.ToString(), option.Name)
    </div>
}

Step 2: The Controller's Save Method

Your existing Save method should work exactly as you have it, but let's show it for completeness:

[HttpPost]
public IActionResult Save(MyModel model)
{
    // model.SelectedIds will hold all GUIDs of the checked checkboxes
    if (model.SelectedIds != null && model.SelectedIds.Any())
    {
        // Do your logic here—save to database, process selections, etc.
    }

    // Redirect or return a view as needed
    return RedirectToAction("Index");
}

Key Notes to Avoid Issues

  • Name Attribute is Non-Negotiable: Every checkbox must have name="SelectedIds"—this is how MVC maps form values to your list property.
  • Guid String Conversion: Always convert the Guid to a string with ToString() (Razor handles this implicitly, but being explicit avoids edge cases).
  • Null Check for Empty Selections: If no checkboxes are selected, model.SelectedIds will be null (not an empty list), so always check for null before accessing it.

That's all there is to it! When you submit the form, MVC's model binder automatically collects all selected checkbox values into your SelectedIds list—no JavaScript required.

内容的提问来源于stack exchange,提问作者Yogurtu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:45:48