UserProfile モデルに新しいフィールド「UserInput」を追加しようとしています。最初に、次を使用して UserProfile テーブルにフィールドを追加しました。
ALTER TABLE [Database].[dbo].[UserProfile]
ADD UserInput nvarchar(max) NULL
すべてがうまくいくようです。次に、それをモデルに追加すると、次のようになりました。
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string FID { get; set; }
public string UserInput { get; set; }
}
しかし、ビューに追加しようとすると、赤い波線が表示され、次のエラーが表示されます。
Error 1 'System.Collections.Generic.IEnumerable<FTv2.Models.UserProfile>' does not contain a definition for 'UserInput' and no extension method 'UserInput' accepting a first argument of type 'System.Collections.Generic.IEnumerable<FTv2.Models.UserProfile>' could be found (are you missing a using directive or an assembly reference?)
私は何か見落としてますか?以前はこの方法でフィールドを追加できましたが、今回見逃したものを誰かが教えてくれるでしょうか? また、コードを実行しようとしましたが、そのページにアクセスしようとするとエラーが発生しました。これは次のとおりです。
Line 14: <legend>Edit</legend>
Line 15: <div class="editor-label">
Line 16: @Html.LabelFor(model => model.UserInput)
Line 17: </div>
Line 18: <div class="editor-field">
助けてくれてありがとう!
編集:問題は、そのビューからフィールドを編集しようとしているという事実にあるようです。 item.UserInput にアクセスしようとすると、正常に返されるためです。コードが機能していない場所は次のとおりです。
@model IEnumerable<FTv2.Models.UserProfile>
using (Html.BeginForm()) {
<fieldset>
<legend>Edit</legend>
<div class="editor-label">
@Html.LabelFor(model => model.UserInput)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserInput)
@Html.ValidationMessageFor(model => model.UserInput)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
ただし、下のセクションでは問題なく機能します。
@{ foreach (var item in Model) {
<p>User Input: @item.UserInputTwo</p>
}}