[英]Passing value from view to controller
OK, i asked a very similar question recently and I got good answers back. 好的,我最近问了一个非常类似的问题,我得到了很好的答复。 However, I probably did not express my problem accurately so I will give it another go here:
但是,我可能没有准确表达我的问题,因此我将在这里再介绍一下:
This is my view 这是我的看法
@using (Html.BeginForm())
{
<h3 class="editable">@Model.Title</h3>
<input type="submit" value="submit">
}
The <h3>
has the class "editable"
which in this case means it can be edited by an inline-editor. <h3>
具有"editable"
类,在这种情况下,它意味着可以由内联编辑器进行编辑。
@Model.Title
Is a properties from my database that I would like to be able to change with the inline-editor . 是我希望能够使用inline-editor更改的数据库属性。
This code would generate the same result: 此代码将产生相同的结果:
@using (Html.BeginForm())
{
<h3 class="editable">@Model.Title</h3>
<input type="text" id="testinput" name="testinput" />
<input type="submit" value="submit">
}
Controller:
[HttpPost]
public ActionResult FAQ(string testInput)
{
page.Title = testInput;
return View();
}
Allthough, this does not use the inline-editor which I would like. 但是,这没有使用我想要的内联编辑器。
Is there maybe a way to treat the <h3>
as if it was a textbox
allowing me to send whatever is in there to the controller? 也许有一种方法可以将
<h3>
视为一个textbox
使我可以将其中的内容发送给控制器吗?
I want to make clear that I do NOT want to send @model.title
to the controller directly. 我想表明我不想直接将
@model.title
发送到控制器。 I want to send the value created by clicking on the <h3>
and using the inline-editor to change it. 我想发送通过单击
<h3>
并使用内联编辑器对其进行更改而创建的值。
Thank you! 谢谢!
When you submit a form in this fashion the controller will try and match it to the correct object type. 当您以这种方式提交表单时,控制器将尝试将其匹配到正确的对象类型。 If you want to just pass back 1 or 2 objects try using the action links.
如果只想传回1或2个对象,请尝试使用操作链接。 These should allow you to pass in the values with names to match your control methods.
这些应该允许您传递带有名称的值以匹配您的控制方法。
View: 视图:
@model MvcApplication2.Models.ShopItem
@{
ViewBag.Title = "Shop";
}
<h2>ShopView</h2>
@using (Html.BeginForm())
{
<h3 class="editable">@Model.Name</h3>
@Html.TextBox("Cost",0.00D,null)
<input type="submit" title="Submit" />
}
Model: 模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication2.Models
{
public class ShopItem
{
public int? Id { get; set; }
public string Name { get; set; }
public decimal? Cost { get; set; }
public ShopItem()
{
Id = null;
Name = "";
Cost = null;
}
}
}
Controller: 控制器:
public ActionResult Shop()
{
ShopItem item = new ShopItem();
return View(item);
}
[HttpPost]
public ActionResult Shop(decimal Cost)
{
ShopItem item = new ShopItem();
item.Cost = Cost;
return View(item);
}
If you put this in the HomeController, to test it. 如果将其放在HomeController中,请对其进行测试。 You will see my input has a strong type, and matches my action inputs
您将看到我的输入具有强类型,并且与我的动作输入匹配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.