加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – html.TextBoxFor和html.Textbox,POSTing值,参

发布时间:2020-12-16 06:30:14 所属栏目:asp.Net 来源:网络整理
导读:好吧,伙计们,需要一些帮助! 我正在使用asp.net mvc3 razor(我相当新,但做了很多网页表单) 好的,这个问题 我的问题围绕提交视图. 我有一个非常复杂的模型,我的视图基于此(强类型). 我想将模型返回到控制器的HttpPost方法中的参数.基本上做: public ActionRe
好吧,伙计们,需要一些帮助!

我正在使用asp.net mvc3 razor(我相当新,但做了很多网页表单)

好的,这个问题

我的问题围绕提交视图.
我有一个非常复杂的模型,我的视图基于此(强类型).

我想将模型返回到控制器的HttpPost方法中的参数.基本上做:

public ActionResult Personal()
    {
        DataModel dataModel = new DataModel();
        FormModel model = new FormModel();
        model.candidateModel = dataModel.candidateModel;
        model.lookupModel = new LookupModel();

        return View(model);
    }

    [HttpPost]
    public ActionResult Personal(FormModel formModel)
    {
        if (ModelState.IsValid)
        {
            //stuff
        }
        return View(formModel);
    }

现在…
我在post方法的formModel参数中获取值时遇到问题.

这工作(意味着我可以看到值)但是很繁琐,因为我必须准确地写出每个字段在字符串中的位置:

@Html.TextBox("formModel.candidateModel.tblApplicant.FirstName",Model.candidateModel.tblApplicant.FirstName)

它呈现如下:

<input name="formModel.candidateModel.tblApplicant.FirstName" id="formModel_candidateModel_tblApplicant_FirstName" type="text" value="Graeme"/>

这不起作用:

@Html.TextBoxFor(c => c.candidateModel.tblApplicant.FirstName)

它呈现如下:

<input name="candidateModel.tblApplicant.FirstName" id="candidateModel_tblApplicant_FirstName" type="text" value="Graeme"/>

现在我假设问题在于id的差异

所以请回答我:

>我是以正确的方式来做这件事的
>为什么textboxfor没有获得正确的值/ id,以及如何让它获得正确的值/ id以便我可以在POST中检索它(如果这甚至是问题)?
>此外,似乎textboxfor是限制性的,如果你有一个日期时间,你如何使用.toshortdate()方法?这让我觉得textboxfor对我没用.

快速澄清:
当我说textboxfor无法正常工作时,我获取表格的时候会得到值.所以他们填写,但在POST /提交,我无法在参数中的formModel中看到它们.

另一方面说明:
没有html帮助器工作,这是问题所在.它们也没有出现在模型状态中.

谢谢大家的帮助

回答:
html.TextBoxFor and html.Textbox,POSTing values,model in parameters

在我的视图中,这是一个问题,我在这个答案中用片段替换了所有代码并且它有效.

再次感谢你

解决方法

Am i going about this the right way

是.

Why doesn’t textboxfor get the right value/id,and how do i make it get the right value/id so i can retrieve it in a POST(if that is even the problem)?

您的代码中还有其他内容使其无效.由于您没有显示所有代码,因此很难说.这是一个完整的工作示例,它说明并证明您的代码还有其他内容:

模型:

public class FormModel
{
    public CandidateModel candidateModel { get; set; }
}

public class CandidateModel
{
    public Applicant tblApplicant { get; set; }
}

public class Applicant
{
    public string FirstName { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new FormModel
        {
            candidateModel = new CandidateModel
            {
                tblApplicant = new Applicant
                {
                    FirstName = "fn"
                }
            }
        });
    }

    [HttpPost]
    public ActionResult Index(FormModel formModel)
    {
        // the username will be correctly bound here
        return View(formModel);
    }
}

视图:

@model FormModel
@using (Html.BeginForm())
{
    @Html.EditorFor(c => c.candidateModel.tblApplicant.FirstName)
    <button type="submit">OK</button>
}

Additionally,it seems that textboxfor is restrictive,in the manner
that if you have a date time,how do you use the .toshortdate()
method? This makes me think textboxfor isn’t useful for me.

我同意TextBoxFor是限制性的.这就是为什么我建议你总是使用EditorFor而不是TextBoxFor.它允许您使用[DisplayFormat]属性和voilà简单地装饰您的视图模型属性.你得到任何你喜欢的格式.

例如:

public class MyViewModel
{
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}",ApplyFormatInEditMode = true)]
    public DateTime CreatedAt { get; set; }
}

并在视图中:

@model MyViewModel
@Html.EditorFor(x => x.CreatedAt)

将完全按照您的预期格式化日期.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读