1

让我有以下视图模型:

public class ViewModel
{
     public string SomeValue {get; set;}
     public int CountryId {get; set;}
}

和国家名单

var countryList = new[]{new[]{Country="Russia", Value=1},new[]{Country="USA", Value=2},new[]{Country="Germany", Value=3} }

我想用这些字段创建一个表单。问题是国家输入必须是文本框。

所以我不能只写类似的东西Html.TextBoxFor(m => m.CountryId)。此类任务的最佳实践是什么?它是隐藏的领域还是别的什么?


编辑

界面看起来像:

    SomeValue: |_______|

    Country:   |_______|

                |Submit button|

在“国家”字段中,我们只能输入国家名称。在 SomeValue 中,其他一些值无关紧要(您可以想象 SomeValue 不存在)。

4

3 回答 3

1

当下拉列表似乎更合适时,这当然是一个奇怪的要求,但我们之前都有过奇怪的要求。:) 这是一个简单的例子,希望能展示你需要知道的一切,以便让它工作。

首先,一个将国家名称与 id 关联起来的简单模型:

public class CountryModel
{
    public int Id { get; set; }
    public string Country { get; set; }
}

现在控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(string country)
    {
        var countryId = GetCountries()
                        .Where(c => c.Country.ToLower() == country.ToLower())
                        .Select(c => c.Id)
                        .SingleOrDefault();

        if (countryId != 0)
            return RedirectToAction("Thanks");
        else
            ModelState.AddModelError("CountryNotSelected", "You have selected an invalid country.");

        return View();
    }

    private List<CountryModel> GetCountries()
    {
        return new List<CountryModel>
        {
            new CountryModel { Id = 1, Country = "Russia" },
            new CountryModel { Id = 2, Country = "USA" },
            new CountryModel { Id = 3, Country = "Germany" }
        };
    }
}

这是视图:

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(false)
    <fieldset>
        <div class="editor-field">
            @Html.TextBox("Country")
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

这里有几点需要注意。首先,您已经声明您在此视图中有额外的属性。在这种情况下,我会将它们放在一个CountryViewModel和模型中绑定到 HTTP POST 方法中的一个实例,而不是简单地绑定到一个字符串。所以,像这样:

public class CountryViewModel
{
    public string SomeValue { get; set; }
    public string SomeMoreFormData { get; set; }
    public string Country { get; set; }
}

然后从这里开始,POST 方法会变成这样:

[HttpPost]
public ActionResult Index(CountryViewModel viewModel)
{
    var countryId = GetCountries()
                    .Where(c => c.Country.ToLower() == viewModel.Country.ToLower())
                    .Select(c => c.Id)
                    .SingleOrDefault();

    if (countryId != 0)
        return RedirectToAction("Thanks");
    else
        ModelState.AddModelError("CountryNotSelected", "You have selected an invalid country.");

    return View(viewModel);
}

其次,请注意我是如何通过GetCountries(). 如果您的需求发生变化,这将允许您稍后轻松地重构它以从数据库中获取国家/地区列表。

于 2012-08-07T10:50:46.380 回答
0
@using (Html.BeginForm("Index", "Home")) {    
<p>
Page number:
@Html.TextBox("CountryId")

<input type="submit" value="CountryId" />
</p>
}

[HttpPost]
public ActionResult Index(int countryId)
{ 
CountryId= countryId;
}
于 2012-08-07T07:18:59.293 回答
0

我不明白你的问题100%。我是否正确地说用户可以输入国家/地区 ID 和名称?

最好将视图模型发送到您的视图。假设您有以下视图模型:

public class YourViewModel
{
     public string CountryName {get; set;}
     public string CountryId {get; set;}
}

您的操作方法可能如下所示:

public ActionResult Index()
{
     YourViewModel viewModel = new YourViewModel();

     return View(viewModel);
}

[HttpPost]
public ActionResult Index(YourViewModel viewModel)
{
     // Check viewModel for nulls
     // Whatever was typed in on the view you have here,
     // so now you can use it as you like
     string country = viewModel.CountryName;
     string id = viewModel.CountryId;

     // Do whatever else needs to be done
}

您的视图可能如下所示:

@model YourProject.DomainModel.ViewModels.YourViewModel

@using (Html.BeginForm())
{
     <div>
          @Html.TextBoxFor(x => x.CountryName)<br />
          @Html.ValidationMessageFor(x => x.CountryName)
     </div>

     <div>
          @Html.TextBoxFor(x => x.CountryId)<br />
          @Html.ValidationMessageFor(x => x.CountryId)
     </div>

     <button id="SaveButton" type="submit">Save</button>
}

我希望这有帮助。

于 2012-08-07T07:48:23.940 回答