在这里使用几个答案,我最终使用了一个元组<>两个模型用于单个视图。为简单起见,tuple.Item1在视图中有一个已知的数字,我将tuple.Item2的信息提交给它的控制器。我想在提交的tuple.Item2中发回tuple.Item1.num的值,用于特定成员tuple.Item2.num。
目前,我有这个用于提交消息:
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "createMessage", @action = "/api/Message" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.Hidden("ID", @Model.Item2.ID)
<div class="editor-field">
@Html.TextArea("Text", @Model.Item2.Text)
@Html.ValidationMessageFor(tuple => tuple.Item2.Text)<br />
</div>
<input type="submit" value="Post Discussion" />
}
因此,我希望在发布到Controller的Item2(消息)模型中发送tuple.Item1.num的值。我该怎么做?
请注意,我对MVC和ASP.net框架不熟悉,所以我可能会混淆一些东西。我知道这个HtmlHelper知道它正在使用MessageController,因为它的@action属性,但是我仍然对如何向它发布值感到困惑。任何帮助都会很棒,谢谢!
根据要求,我的模特:
DrugEntry.cs
namespace Project.Models
{
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using FileHelpers;
[DelimitedRecord(",")]
[Table("DRUGS")]
public class DrugEntry
{
private string ndc;
private string dosage;
private string brand;
private string generic;
private string currentStatus;
public DrugEntry()
{
this.ndc = string.Empty;
this.dosage = string.Empty;
this.brand = string.Empty;
this.generic = string.Empty;
this.currentStatus = "good"; // By default, a drug has no shortages associated with it, and thus is considered 'good'
}
public DrugEntry(string ndc, string dosage, string brand, string generic, string currentStatus)
{
this.ndc = ndc;
this.dosage = dosage;
this.brand = brand;
this.generic = generic;
this.currentStatus = currentStatus;
}
[Key]
[Column("NDC")]
public string NDC
{
get
{
return this.ndc;
}
set
{
this.ndc = value;
}
}
[Column("DOSAGE")]
public string Dosage
{
get
{
return this.dosage;
}
set
{
this.dosage = value;
}
}
[Column("BRAND_NAME")]
public string Brand
{
get
{
return this.brand;
}
set
{
this.brand = value;
}
}
[Column("GENERIC_NAME")]
public string Generic
{
get
{
return this.generic;
}
set
{
this.generic = value;
}
}
[Column("CURRENT_STATUS")]
public string CurrentStatus
{
get
{
return this.currentStatus;
}
set
{
this.currentStatus = value;
}
}
}
}
Message.cs
namespace Project.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using FileHelpers;
[Table("MESSAGE")]
public class Message
{
private int id;
private int shortageID;
private string ndc;
private string user;
private DateTime date;
private string text;
[Key]
[Column("ID")]
public int ID
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
[Column("SHORTAGE_ID")]
public int ShortageID
{
get
{
return this.shortageID;
}
set
{
this.shortageID = value;
}
}
[Column("NDC")]
public string NDC
{
get
{
return this.ndc;
}
set
{
this.ndc = value;
}
}
[Column("USER")]
public string User
{
get
{
return this.user;
}
set
{
this.user = value;
}
}
[Column("DATE")]
public DateTime Date
{
get
{
return this.date;
}
set
{
this.date = value;
}
}
[Column("TEXT")]
public string Text
{
get
{
return this.text;
}
set
{
this.text = value;
}
}
}
}
答案 0 :(得分:1)
我建议使用ViewModel,而不是使用元组。 ViewModel 只是您专门为满足视图要求而创建的简单类。
ViewModel是Asp Mvc标准,您不需要为视图修改模型,而是创建ViewModel。
您可以像这样设置视图模型。
// You typically name your ViewModel to the View
// that it represents.
public class MessageSubmission
{
public Message Message { get; set; }
public DrugEntry DrugEntry { get; set; }
}
ViewModels应存储在自己的文件夹中。创建一个名为 ViewModels 的文件夹并将其存储在那里。以下是Microsoft创建的应用程序的文件夹结构,请注意ViewModels文件夹?
由于您使用的是弱类型的html扩展,我建议如下。
@model MyMvcApplication1.ViewModels.MessageSubmission
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "createMessage", @action = "/api/Message" }))
{
@Html.ValidationSummary(true)
@Html.Hidden("ID", @Model.Message.ID)
<!-- // You can assign the DrugEntry.NDC, to a Message.NDC like this -->
@Html.Hidden("NDC", @Model.DrugEntry.NDC)
<div class="editor-field">
@Html.TextArea("Text", @Model.Message.Text)
@Html.ValidationMessageFor(model => model.Message.Text)<br />
</div>
<input type="submit" value="Post Discussion" />
}
只需像平常一样设置控制器。
[HttpPost]
public ActionResult Message(Message message)
{
// Add your stuff here.
}
MVC默认模型绑定器自动将视图页面(ID,NDC,Text)中的值分配给控制器中的等待模型(Message.ID,Message.NDC,Message.Text)
活页夹通过将html控件的ID与模型的属性进行比较来对齐字段。
View | Model Properties
------------------------------
Hidden.ID | Message.ID
Hidden.NDC | Message.NDC
TextArea.Text | Message.Text
答案 1 :(得分:0)
你可以在ajax中试试这个
@using (Ajax.BeginForm("ActionOfItem2", "ControllerOfItem2", new AjaxOptions { UpdateTargetId = "UpdateDiv"}))
{
@Html.Hidden("ID", @Model.Item2.ID)
<input type="submit" value="Post Discussion" />
}
控制器
public ReturnType ActionOfItem2(string ID)
{
// Use the ID here
}