C# 如何在MVC4中提交空值?

C# 如何在MVC4中提交空值?,c#,asp.net-mvc,forms,asp.net-mvc-4,C#,Asp.net Mvc,Forms,Asp.net Mvc 4,我正在MVC4,C中设计一个搜索页面。我的视图中有以下搜索表单: @using (Html.BeginForm("Search", "Vragen", FormMethod.Get)) { <div id="advanced"> <h1 name="advanced"> <span>Geavanceerd zoeken</span> </h1> <div class="green">

我正在MVC4,C中设计一个搜索页面。我的视图中有以下搜索表单:

@using (Html.BeginForm("Search", "Vragen", FormMethod.Get)) { 
 <div id="advanced">
    <h1 name="advanced">
        <span>Geavanceerd zoeken</span>
    </h1>
    <div class="green">
        <span>
            <input placeholder="Trefwoord" class="geav" type="text" name="tref" id="textfield" /><br />
            <input placeholder="Politicus" class="geav" type="text" name="pol" id="textfield" /><br />
        </span>
        <span>
            <select class="geav" name="partijId">
                <option value="">-Partij-</option>

                @foreach (var partij in partijen) { 
                    <option value="@partij.partijID">@partij.naam</option>
                }
            </select> 
            <br />
            <select class="geav" name="parlementId">
                <option value="">-Niveau-</option>

                @foreach (var parlement in parlementen) {
                    <option value="@parlement.parlementID">@parlement.naam</option> 
                }
            </select> 

        </span>
        <span>
            <select class="geav" name="themaId">
                <option value="">-Thema-</option>

                @foreach (var thema in themas) { 
                    <option value="@thema.themaID">@thema.naam</option>

                    List<Subthema> subthemas = themaMapper.getAlleSubthemas(thema.themaID);

                    foreach (var subthema in subthemas) { 
                        <option value="@subthema.subthemaID">@subthema.naam</option>
                    }
                }
            </select>
    ...
这不是完整的表单,如果我以empty=默认值将其提交给我的searchcontroller,则url如下所示: localhost:62488/Vragen/Search?tref=test&pol=&partijId=&parlementId=&themeid=&kieskringId=

对于空变量,请注意pol=$partijId=&。。。
我希望这些值在保留为空时取消显示,这样我就有空值。。。我怎样才能做到这一点?

获得您想要的东西的方法是在您的操作中接收模型,而不是单个值。只需构建这样一个类:

public class SearchModel
{
    public string tref { get; set; }
    public string pol { get; set; }

    ...
}
然后在你的行动中做到:

public ActionResult Search(SearchModel model)

改用f0om方法post..我能想到的唯一解决方案是使用一些javascript黑魔法。你真的需要这项功能吗?我需要这项功能,因为出于某种原因,我无法从url值中获取。。。使用viewcontext.values。。使用post似乎不起作用,Ehsan会删除所有参数,甚至是填写的参数