该值"(字符串)"是无效的 [英] The value "(string)" is invalid

查看:161
本文介绍了该值"(字符串)"是无效的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个包含两个文本框,用户可以在一些数据输入视图。一个文本框只允许1-10,另外一个字符串值。我不知道是什么code修改我做了,但第二个文本框接受一个字符串,不再是作品。例如,当我在一个字符串输入,并尝试提交表单,我得到指出值(字符串)验证消息是无效的。下面是我的解决方案的一些code片段。

I currently have a view that contains two text boxes where users can enter in some data. One text box only allows values of 1-10, the other a string. I am not sure what code change I made, but the second text box accepting a string no longer "works". For example, when I enter in a string and try to submit the form, I get a validation message that states "The value "(string)" is invalid. Below are some code snippets in my solution.

实体:

public class MovieReview
{
    public int Id { get; set; }

    [Range(1, 10)]
    [Required]
    public int Rating { get; set; }
    public string Review { get; set; }
    public int MovieId { get; set; }
}

控制器:

public class ReviewsController : Controller
{
    private MovieLoversDb _db = new MovieLoversDb();

    public ActionResult Index([Bind(Prefix = "id")]int movieId)
    {
        var movie = _db.Movies.Find(movieId);
        if (movie != null)
        {
            return View(movie);
        }
        return HttpNotFound();
    }

    [HttpGet]
    public ActionResult Create(int movieId)
    {
        return View();
    }

    [HttpPost]
    public ActionResult Create(MovieReview review)
    {
        if (ModelState.IsValid)
        {
            _db.MovieReviews.Add(review);
            _db.SaveChanges();
            return RedirectToAction("Index", new { id = review.MovieId });
        }
        return View(review);
    }

局部视图:

@model MovieLovers.Models.MovieReview

@{
    ViewBag.Title = "Review a Movie";
}

<h2>Review a Movie</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

<fieldset>
    <legend>New Review</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Rating)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Rating)
        @Html.ValidationMessageFor(model => model.Rating)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Review)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Review)
        @Html.ValidationMessageFor(model => model.Review)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ReviewerName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ReviewerName)
        @Html.ValidationMessageFor(model => model.ReviewerName)
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

现在的问题是,我究竟做错了什么?这是为什么验证错误发生?

The question now is, what am I doing wrong? Why is that validation error generating?

推荐答案

我想通了下原来的职位评论利用贾森的建议。它好像评论可能已被使用了两次,但我找不到在哪里。我改变了属性名称为体,现在它的作品。

I figured it out using a suggestion by Jasen in comments under the original post. It seems as though "Review" may have been used twice, although I could not find where. I changed the property name to "body" and now it works.

感谢您的帮助!

这篇关于该值&QUOT;(字符串)&QUOT;是无效的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆