With the help of several SO questions, I've figured out how to use two models on the same view, using the tuple form. At the top of my file is this:
@using Project.Models;
@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_Layout.cshtml";
@model Tuple<Foo, Bar>
}
For the Foo stuff, it uses jQuery like this:
@Html.DisplayFor(tuple => tuple.Item1.ID)
and works fine. However, for my second model, it isn't displaying info, but is a submission form. Currently, this is what I have:
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "createFoo", @action = "/api/Foo" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="editor-field">
@Html.TextAreaFor(tuple => tuple.Item2.Text)
@Html.ValidationMessageFor(tuple => tuple.Item2.Text)<br />
</div>
<input type="submit" value="Post Response" />
}
Mind you, this is mostly copy paste from other views since I'm new to MVC and it worked fine with other forms. For my FooController, I have this:
public void Post([FromBody] Foo foo)
{
Foo existingFoo = this.fooRepository.GetFoo(foo.ID);
if (existingFoo != null)
{
// throw error
}
else
{
System.Diagnostics.Debug.WriteLine("MESSAGE POSTING: " + foo.Text);
}
}
When submitting from the view, the received foo isn't null (checked in other tests), but the foo.text is empty. I've tried lots of different inputs and such, but I'm just so unfamiliar with the @Html.* functions and ASP.net-MVC in general that I'm not sure where I could be going wrong.
If you need to see my repository code let me know, but I doubt it'd have an effect on this. Thanks in advance for any help.